Browsing 7239 questions and answers with Jon Skeet

Why does SortedSet<T> ignore CompareTo() values of 0 by default?

When I try to AddRange to a SortedSet, it doesn't add values when their compareto result is zero. This doesn't make any sense to me, as it's not a SortedSet of the values that...
Jon Skeet
people
quotationmark

The whole point of the comparison is to say whether one item is greater than, less than or equal to another. A set doesn't allow equal values - and in a sorted set, equality is defined by the comparison. It's as simple as that. For your... more 2/19/2016 6:10:26 PM

people

C# Can't access public function from different class

I'm starting with C# and I got a problem. I created a "Windows Form Application" project. I have a form class named form1 and I added a UserControl class named usercontrol1. In...
Jon Skeet
people
quotationmark

This is because you've declared your variable to be of type UserControl. That means the compiler will only let you use members declared in UserControl and the classes it inherits from. The actual object is still of type usercontrol1 at... more 2/19/2016 5:32:30 PM

people

Why does the "as" operator not use an implicit conversion operator in C#?

I have defined implicit string conversion from/to a certain type in C# (dummy code): public class MyType { public string Value { get; set; } public static implicit...
Jon Skeet
people
quotationmark

Why does the soft cast not use the implicit converter? Well, that's the way the language is specified, basically. From the C# 5 specification section 7.10.11: If the compile-time type of E is not dynamic, the operation E as T... more 2/19/2016 2:53:58 PM

people

Encoding errors in embedded Json file

I have run into an issue and can't quite get my head around it. I have this code: public List<NavigationModul> LoadNavigation() { byte[] navBytes =...
Jon Skeet
people
quotationmark

Here's a simpler approach, removing the BOM after decoding: // Your data is always in UTF-8 apparently, so just rely on that. string text = Encoding.UTF8.GetString(data); if (text.StartsWith("\ufeff")) { text =... more 2/19/2016 2:47:50 PM

people

Unpredictable results when using Parallel.For

I am running a Parallel for loop which initially runs for the times = number of processors and performs a long running operation. Each task when finished, checks for more tasks...
Jon Skeet
people
quotationmark

I see various problems at the moment: Parallel.For is just starting the tasks. It won't wait for them to complete. It will wait for the DoSomething method calls to return, but they're returning tasks representing the asynchronous... more 2/19/2016 7:16:23 AM

people

UWP 10 C# HttpResponseMessage task freezes

I couldn't find from search anyone having similar issues so: I'm trying to get XML from server with HttpClient, but my UI freezes weirdly at line "task.Wait()". This is the...
Jon Skeet
people
quotationmark

It's not freezing weirdly at all - it's freezing entirely reasonably. You're calling task.Wait(), which stops your UI thread from doing any more work until that task has completed. However, that task itself needs to get back to the UI... more 2/19/2016 7:10:51 AM

people

C# addition operator overloading

I am trying to port some C# code to another language (C# is not my usual language). I have a vector class (cpVect) that overrides the '+' operator twice: public static...
Jon Skeet
people
quotationmark

Both call the binary operator. The unary operator would be called if you only had one operand: v3 = +v1; The unary + operator is rarely useful, to be honest - but it's mostly there for symmetry with the unary - operator. more 2/18/2016 9:22:34 PM

people

C# Left Outer Join lambda expression error

I have the following tables: Table1 { Code //string Desc //string } Table2 { Code //string Value //decimal? } I need to Left Join the tables and for...
Jon Skeet
people
quotationmark

You just need to specify the type of the null value in your anonymous type initializer: .DefaultIfEmpty(new { x.Code, x.Desc, Value = (decimal?) null })) When you used 0, you were creating a separate anonymous type that had a Value... more 2/18/2016 1:29:49 PM

people

What does "=>" do in .Net C# when declaring a property?

I've seen this kind of property declaration in a .NET 4.6.1 C# project public object MyObject => new object(); I'm used to declaring read only properties like this: public...
Jon Skeet
people
quotationmark

The first uses the new-to-C#-6 expression-bodied member syntax. It's equivalent to: public object MyObject { get { return new object(); } } The second is also new to C# 6 - an automatically implemented read-only property. It's... more 2/18/2016 1:12:27 PM

people

How to remove compiler error with struct: "Use of unassigned local variable"

The C# compiler is a bit ... old fashioned ... and won't do static analysis. So it breaks on seemingly correct code like this: MyStruct s; bool inited = false; foreach( Something...
Jon Skeet
people
quotationmark

Is there a code way to tell it to shut up and mind its own business? The compiler's business is implementing the C# specification. The code you've written should not compile according to the C# specification. The s.DoSomething() call... more 2/18/2016 1:08:14 PM

people