Browsing 15836 questions and answers with Jon Skeet

Check all list of values are in the enum c#

I have an integer list containing the ids List<int> ids = new List<int>; I am adding values in the list list.Add(100); list.Add(110); list.Add(120); I want to...
Jon Skeet
people
quotationmark

Enum.TryParse returns true for any numeric value. As per the documentation: If value is the string representation of an integer that does not represent an underlying value of the TEnum enumeration, the method returns an enumeration... more 8/1/2017 9:52:15 AM

people

IEnumerable<type> is false in code but true in Immediate Window?

Debugging some code I came across an "is IEnumerable" comparison, which confusingly evaluates to false in code but true in the Immediate Window. I wonder if anyone can shed...
Jon Skeet
people
quotationmark

It's a quirk of the intermediate window, basically. There are some pieces of code which evaluate differently there - it's one reason I generally prefer not to use the intermediate window. An IEnumerable<Fruit> isn't an... more 8/1/2017 8:50:10 AM

people

(Func, Action, Predicate) VS Converter and Comparison delegate c#

I understood Func, Action, Predicate with the help of some amazing posts available on this site and in short- Action is a delegate (pointer) to a method, that takes zero, one or...
Jon Skeet
people
quotationmark

Now I'm confused in Converter and Comparison delegate,why .Net Framework introduced these 2 delegate flavours for just converting and comparing, when and how to use these 2 over existing 3. Historically, the delegates were introduced... more 8/1/2017 7:35:46 AM

people

Waiting for Tasks to finish

I have 2 simple methods and I want process to not continue until they are finished completely. Because of this I used await Task.WaitAll(tasks); but compiler is giving me error in...
Jon Skeet
people
quotationmark

I suspect you meant WhenAll rather than WaitAll. WaitAll is a blocking call that returns all the results synchronously, when the original tasks have completed. You can't await that result, as it's not a task. WhenAll is an asynchronous... more 7/31/2017 1:35:02 PM

people

What is the reason that Encoding.UTF8.GetString and Encoding.UTF8.GetBytes are not inverse of each other?

Probably I am missing something, but I do not understand why Encoding.UTF8.GetString and Encoding.UTF8.GetBytes are not working as inverse transformation of each other? In the...
Jon Skeet
people
quotationmark

They're inverses if you start with a valid UTF-8 byte sequence, but they're not if you just start with an arbitrary byte sequence. Let's take a concrete and very simple example: a single byte, 0xff. That's not the valid UTF-8 encoding for... more 7/31/2017 7:55:37 AM

people

What is the difference between these two variations of collection initialiser expressions?

I've been using C# for a while, but recently noticed that the behaviour of one of my unit tests changed depending on which variation of collection initialiser expression I...
Jon Skeet
people
quotationmark

Yes, your assumption is accurate. If an object initializer just has: { Property = { ... } } rather than { Property = expression } then the setter for the property isn't used - the getter is used, and then either the Add... more 7/31/2017 7:38:26 AM

people

Same class name in multiple namespaces in Visual Studio

How to know if there is a class name repeated in different namespaces in Visual Studio? Obviously I'm not looking to test every class name manually.
Jon Skeet
people
quotationmark

I'd just write a unit test that uses reflection. Something like: var duplicateGroups = typeof(SomeTypeInAssembly).GetTypes() .GroupBy(t => t.Name) .Where(g => g.Count() != 1); // Assert that duplicateGroups is... more 7/31/2017 7:07:34 AM

people

Type mismatch convert char[] to an object

I'm sure I'm missing something simple but this problem seems absolutely stupid. private static void method501(char ac[]) { char ac1[] = ac.clone(); } My problem is that the...
Jon Skeet
people
quotationmark

This happens in Eclipse if you've got your compiler settings to target very old source compatibility. With a compatibility level of 1.5 or above, it's fine - but if you set the source compatibility level to 1.3 or 1.4, you'll get this... more 7/29/2017 3:51:34 PM

people

Find a value in a binary tree avoiding stackoverflow exception

I'm trying to find a value in a binary tree and returning the node that has the value I'm looking for. I did an algorithm that works well when the value is not in a very deep...
Jon Skeet
people
quotationmark

The simplest approach is to convert this into a while loop, which just maintains state of "the current node we're testing". On each iteration of the loop, there are three possibilities: The current node has the right value, in which... more 7/28/2017 1:00:51 PM

people

Different delegates in loop

Is it possible to have delegates declared in a loop perform different actions? var buttons = new List<ImageButton>(); buttons.Add(FindViewById<ImageButton>(Resource.Id.button1)); buttons.Add(FindViewById<ImageButton>(Resource.Id.button2)); int count = 1; foreach(var button in buttons) { button.Click += delegate { Toast.MakeText(this, "I am " + count, ToastLength.Short).Show(); } count++; } The toast message always "I am 2" when clicking either button. Ultimately I would like to have a number of buttons performing slightly different actions when clicked.
Jon Skeet
people
quotationmark

You've got a single count variable, and your anonymous method captures it. That means when the delegate is executed, it will always use the current value of that variable. You want "the value of count when the delegate was created" which... more 7/28/2017 10:50:17 AM

people