Browsing 7239 questions and answers with Jon Skeet

What does the variable hold if there is no match after running a LINQ

[WebMethod] public List<FavoritesTO> getFavorites(string username) { using (FavoritesDataContext db = new FavoritesDataContext ()) { var query = from row in...
Jon Skeet
people
quotationmark

The value of query will just be the query - until you try to evaluate the results, nothing knows whether or not there are any. After that, I suspect that LINQ to SQL will cache the results - but it's still conceptually just a query. If... more 4/11/2014 1:56:36 AM

people

Difference between Task and async

C# provides two ways of creating asynchronous methods: Method 1: static Task<string> MyAsyncTPL() { Task<string> result = PerformWork(); return...
Jon Skeet
people
quotationmark

await is basically a shorthand for the continuation, by default using the same synchronization context for the continuation. For very simple examples like yours, there's not much benefit in using await - although the wrapping and... more 4/10/2014 7:46:08 PM

people

Why .NET group by is (much) slower when the number of buckets grows

Given this simple piece of code and 10mln array of random numbers: static int Main(string[] args) { int size = 10000000; int num = 10; //increase num to...
Jon Skeet
people
quotationmark

I'm pretty certain this is showing the effects of memory locality (various levels of caching) and alos object allocation. To verify this, I took three steps: Improve the benchmarking to avoid unnecessary parts and to garbage collect... more 4/10/2014 4:47:30 PM

people

Overriding function with exact same content causes error

The title may be a bit misleading but I was not sure how to word this. I have 2 classes, BinarySearchTree and HashTreeSet. I am currently using HashTreeSet to implement a...
Jon Skeet
people
quotationmark

You've got two different Node<T> classes - one as an inner class of BinarySearchTree and one as an inner class of HashTreeSet. They're completely different classes, so your override is trying to return an unrelated type to the return... more 4/10/2014 2:24:16 PM

people

Why is DocumentBuilder.parse() not working

I have read several posts about how to use the DocumentBuilder.parse() function for getting a document object. Document document = builder.parse(new InputSource(new...
Jon Skeet
people
quotationmark

As soon as this executes, the sr.str value becomes null and same with is.characterInputStream.str. This seems odd to me, is this expected? Yes, I'd say so. DocumentBuilder.parse is closing the reader. StringReader.close() sets str to... more 4/10/2014 2:13:59 PM

people

Why does this precision fail example show up with doubles but not with floats?

There's a great discussion on SO about why floating-point should not be used for currency operations and a lovely example is given here from Bloch's Effective Java. I was playing...
Jon Skeet
people
quotationmark

There are eight interesting values involved here (four for each type). Their exact values are: Double values 1.03d: 1.0300000000000000266453525910037569701671600341796875 0.42d: ... more 4/10/2014 1:08:31 PM

people

Convert.ToString(Decimal, IFormatProvider) or String.Format

i've seen the question Odd decimal type behavior for ToString(IFormatProvider) this is my test code // Define an array of numbers to display. double[] numbers = { -1.5345e16,...
Jon Skeet
people
quotationmark

As per the documentation for Convert.ToString(double, IFormatProvider): This implementation is identical to Double.ToString(IFormatProvider) ... which is then documented as: The ToString(IFormatProvider) method formats a Double... more 4/10/2014 12:58:05 PM

people

reading from a particular part of a line to the end (JAVA)

I have a text file. each line is a slightly different length. i want to read for the 44th character of each line to the end of the line. working fine for some line but getting a...
Jon Skeet
people
quotationmark

i want to read for the 44th character of each line to the end of the line Then just call String.substring(int): String temp = line.substring(44); I strongly suspect the issue with your current code is that it's expecting to get 2... more 4/10/2014 12:20:42 PM

people

Java: Enum value at compile time?

How do I fetch the value string of an enum at compile time? If I do EnumType.EnumVar.value(), it's resolved at runtime. I want this is to be obtained at compile time so that I can...
Jon Skeet
people
quotationmark

You don't, basically. If you want to use enums in a switch, you should parse the string value to an enum value, and then switch on that. If the value may not be in the enum, consider creating a Map<String, EnumType> so that you can... more 4/10/2014 6:23:35 AM

people

How To Set OptionalAttribute of Optional Parameter In C# to The Current Directory?

I am using the new .Net 4.5 feature for specifying Optional parameters as an OptionalAttribute (see MSDN) in the method signature like so. public void GenerateReport(int...
Jon Skeet
people
quotationmark

You can't. An optional parameter's default value is stored in IL as a constant. "The current directory" is not a constant, so it can't be used as a default value. The closest you can easily come is to make the optional value null, and use... more 4/9/2014 7:26:20 PM

people