Browsing 7239 questions and answers with Jon Skeet

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

Can we change wrapper class into primitive data types?

can we change wrapper to primitive ?if not then what is happening in this code int I = Integer.valueOf(46); System.out.println(I); I am not getting any error.
Jon Skeet
people
quotationmark

Yes, this is called unboxing: Integer boxed = 10; // Boxing int unboxed = boxed; // Unboxing Boxing conversions are described in JLS 5.1.7; unboxing conversions are described in JLS 5.1.8. Note that if you try to unbox a null... more 7/28/2017 6:27:08 AM

people

Type casting in Java from char to short

Why can we not pass a char value as an argument to any method that accepts a short parameter, whereas we can pass a char value to another method whose parameter is of int type?...
Jon Skeet
people
quotationmark

Why can we not pass a char value as a argument to any method that accepts a short parameter Because there's no implicit conversion from char to short in an invocation context. whereas we can pass a char value to another method... more 7/27/2017 1:44:23 PM

people

Copy array of one type to array of different type

I was wondering if it's possible to use some sort of Array.Copy method in C# to copy elements of one array to and array of same size but different type. I get a struct from a...
Jon Skeet
people
quotationmark

For some reason, neither Array.Copy nor Buffer.BlockCopy is happy to copy from an int[] to a Foo[], although Array.Copy can go in the other direction perfectly happily. It looks to me like your options are: The kind of copy you showed... more 7/27/2017 8:43:52 AM

people

ValueTuple not working in Razor views with MVC 5

I have an ASP.NET MVC 5 project in Visual Studio 2017 and after successfully adding C# 7 support (which has worked for me so far), I tried using tuples in my Razor views, but I...
Jon Skeet
people
quotationmark

Of course I have installed System.ValueTuple as a nuget package I suspect that's the problem. My guess is that even though you're targeting .NET 4.5.2, you're running on a .NET 4.7 system, and that includes System.ValueTuple in... more 7/26/2017 7:30:09 PM

people

C# populating class array from xml linq results in FormatException

I'm getting a FormatException error "Input string was not in a correct format." error... Employee class is all strings except the Status(int), Type(int) and...
Jon Skeet
people
quotationmark

By calling ToString on an XElement, you're ending up with the whole element as a string, e.g. <Status>4</Status> ... and trying to parse that as an int. I would strongly suggest: Just using the Value property or a cast to... more 7/26/2017 4:10:41 PM

people