Browsing 7239 questions and answers with Jon Skeet

Finding an element with Linq and lambda expressions: but doesn't work

Have a look to the following example: The first solution, using the foreach, works pretty well and easily. But I was trying to write it using Linq and I could not achieve this...
Jon Skeet
people
quotationmark

It looks like you want: PlacedSelection actualSelection = Model.Coupon.Categories .SelectMany(cat => cat.Value.Selections) .FirstOrDefault(selection => selection.EventId == Model.EventId); Any would be used if you were... more 10/3/2014 9:51:54 AM

people

Unity enums referenced by inspector becoming out of order when extended

Issue: enum's referenced by gameobject scripts via inspector variables become out of order when the enum has new entries added before the referenced index. Details: So I have...
Jon Skeet
people
quotationmark

Just specify explicit numeric values for the entries: public enum StringID { String_EMPTY = 0, String_Inventory = 1, String_Recipes = 2, String_Tools = 3, String_Journal = 4, //... } That way the ordering is... more 10/3/2014 9:25:41 AM

people

Async/await exception and Visual Studio 2013 debug output behavior

I'm developing an application in C# that communicates with Dynamics NAV through web services. To reduce duplicate code and because there will be many endpoints, I have created a...
Jon Skeet
people
quotationmark

When an exception occurs in an async method, it doesn't just propagate up the stack like it does in synchronous code. Heck, the logical stack is likely not to be there any more. Instead, the exception is stored in the task which... more 10/3/2014 6:03:23 AM

people

Which one is more preferrable and why

Which of the following code is better and why? I have seen many programs written in the first way shown below. Why is it done so? First Way: public static final int SIZE =...
Jon Skeet
people
quotationmark

There are two benefits in extracting the constant into a static final field: Firstly, it has a name - admittedly SIZE isn't a very good one (size of what? why?) but in other cases it can be very useful to make your intent clear. For... more 10/3/2014 5:51:21 AM

people

Java 8 Lambda Filter collection by another collection

I have a Set<String> usernames and List<Player> players I would like to filter out those players that are not in the Set. I know how to do this in Vanilla pre Java...
Jon Skeet
people
quotationmark

You've got the lambda expression in the wrong place - the whole of the argument to filter should be the lambda expression. In other words, "Given a player p, should I filter it or not?" players.stream().filter(p ->... more 10/2/2014 9:56:30 PM

people

Complexity of insert(0, c) operation on StringBuffer: is it O(1)?

I am aware that the append() operation for StringBuffer takes O(1) time and it avoids the overhead of creating multiple copies of String objects as compared to String...
Jon Skeet
people
quotationmark

Well, it's an implementation detail - but I wouldn't expect it to be a linked list of characters. I'd expect it to be a char[] with a length, basically - like an ArrayList, but for characters. So inserting a character at the start of the... more 10/2/2014 9:51:53 PM

people

TimeZone getAvailableIDs() filter only tzdb timezones

String[] available = TimeZone.getAvailableIDs(); The above piece of code in Java returns both TZDB format timezones and Windows format timezones. Is there a way to make it...
Jon Skeet
people
quotationmark

It sounds like you should probably just filter out time zone IDs which don't have a / in. TZDB contains zone IDs such as EET and EST - for example, version 2014a contains the follow IDs of 4 characters or fewer: "CET", "Cuba", "EET",... more 10/2/2014 9:33:36 PM

people

Generate Unique Random Number

I know similar questions have been asked, but I have a rather different scenario here. I have a SQL Server database which will store TicketNumber and other details. This...
Jon Skeet
people
quotationmark

This answer assumes you can't change the requirements. If you can use a hi/lo scheme to generate unique IDs which aren't random, that would be better. I assume you've already set this as a primary key in the database. Given that you've... more 10/2/2014 9:09:27 PM

people

Disable C# optimization of loop return value

I have a method that returns an IEnumerable<T>. I call this method form two places, and in one of those places I do not do anything with the results. It looks like the C#...
Jon Skeet
people
quotationmark

It looks like the C# compiler removes the call to that method No, it doesn't - this is just how iterator blocks work. The code inside the iterator block will only start being executed when MoveNext() is called for the first time. If... more 10/2/2014 12:37:33 PM

people

Creating dictionaries with pre defined keys C#

I'm looking for a way to define a dictionary for reuse. ie. I can create the dictionary object without having to populate it with the values I want. Here is what I have currently...
Jon Skeet
people
quotationmark

It's not really clear whether you're concerned about the amount of code you've written, or the efficiency of it. From an efficiency perspective, it's fine - it's O(N), but that's hard to avoid if you're populating a dictionary with N... more 10/2/2014 12:18:08 PM

people