Browsing 7239 questions and answers with Jon Skeet

Implementation of decimal in C#. Is it possible to amend it to support repeating decimals?

There is this problem with C# decimal type: Console.WriteLine( 1m/3 + 1m/3 == 2m/3 ); False It can represent numbers exactly only when dividing by 2 or 5, otherwise result...
Jon Skeet
people
quotationmark

decimal is implemented as a significand, sign and exponent, basically - a floating point number, where the "point" is a decimal point instead of a binary point (as per float and double). See my article on the topic for more details. (Note... more 11/8/2013 3:37:04 PM

people

How to add an element to a null Set<class>?

I've got a set of elements of a custom class User. Now I simply cannot figure out, how this class could be instantiated. I even tried it by creating another object and pulling the...
Jon Skeet
people
quotationmark

There's a huge difference between a reference to an empty set, and a null reference. (Just like there's a huge different between a reference to the empty string, and a null reference.) It sounds like you should probably change your Site... more 11/8/2013 3:23:21 PM

people

Converting string to integer, but preceding zero is being removed Follow up

This question has been answered so please close it... Thanks for the clarifications!! I looked at the question above but there is an use case which we should consider before...
Jon Skeet
people
quotationmark

Simply put, an integer doesn't have a number of leading zeroes. It doesn't even have information about whether it's decimal, hex, or anything like that. It's just an integer. If you really need to follow your existing design, I suggest... more 11/8/2013 2:57:14 PM

people

How can I get the following code so that it debugs without error?

This code that I've written is coming up with errors that I don't understand how to fix. Here's what I want it to do: Create 3 cases in which the user is allowed to choose between...
Jon Skeet
people
quotationmark

For a start both of these look wrong: for (int column = 0; column < array[row].length; column++); if (arrayelement%2 == 0) || (arrayelement%5 == 0); Here you're using a ; as the entire body of both the if statement and the for loop.... more 11/8/2013 2:48:45 PM

people

Necessity to allocate array for collection to array conversion in java using generics

Why is it necessary to allocate an array that can hold the values of a collection and pass it to the toArray(T []) method of this collection? I know there is the toArray() method...
Jon Skeet
people
quotationmark

Why is it necessary to allocate an array that can hold the values of a collection and pass it to the toArray(T []) method of this collection? It isn't. You can do so if you want to, but equally you can just pass in an empty array.... more 11/8/2013 1:47:30 PM

people

HttpURLConnection getInputStream() has one second delay

I am using HttpURLConnection for making POST requests. I observe always the same behaviour during tests: first request runs very fast (miliseconds) all following requests take...
Jon Skeet
people
quotationmark

You're never closing the input stream from the connection - that may mean that the connection isn't eligible for pooling, and on the next attempt it's waiting for up to a second to see if the previous request's connection will become... more 11/8/2013 1:45:08 PM

people

Check which class has called function

How can I check which class has called my method? For example: If Class A uses the MethodB in the Class C than, the function should do something else than the function would do,...
Jon Skeet
people
quotationmark

There's no good way of doing this - and it's fundamentally a bad design, IMO. If ClassA and ClassB want different things to happen, they should call different methods. The only time this is reasonable in my experience is when trying to... more 11/8/2013 1:29:06 PM

people

Are private methods really safe?

In Java the private access modifier consider as safe since it is not visible outside of the class. Then outside world doesn't know about that method either. But I thought Java...
Jon Skeet
people
quotationmark

It depends on what you mean by "safe". If you're running with a security manager that allows this sort of thing, then yes, you can do all kinds of nasty things with reflection. But then in that kind of environment the library can probably... more 11/8/2013 10:25:23 AM

people

C# Count indentation level XML

I want to count the indentation level of a generated XML file. I found some code that could recursively go through the document. But I can't seem to find a way to get the number...
Jon Skeet
people
quotationmark

The simplest way to get the indentation level of a specific element is to see how many parent levels it has: int GetDepth(XElement element) { int depth = 0; while (element != null) { depth++; element =... more 11/8/2013 10:19:39 AM

people

How to solve this compile error?

I get this error "no enclosing instance of type oppgave1 is accessible", when i wrote "new Triangle();". I need som help to explain me where i did wrong (about the triangle...
Jon Skeet
people
quotationmark

Triangle is currently an inner class - which means you can only create it by also having an instance of the enclosing class. Simple options: Make Triangle a top-level (non-nested) class. Make it a static nested class Provide an instance... more 11/8/2013 10:12:38 AM

people