Browsing 7239 questions and answers with Jon Skeet

Attempting to assign to a C# 'foreach iteration variable' generates a compile time error. Doing so in a Linq [IEnumerable].ForEach(...) does not. Why?

I noticed what I believe to be strange behaviour in C# and Linq today when trying to make a class a bit tidier and remove a for loop, used to initialise an array of strings. My...
Jon Skeet
people
quotationmark

Yes, it does nothing - but there are lots of cases where that happens in C#. It's not clear where you'd expect it to be an error, or what you'd expect it to achieve. Basically your lambda expression is a shorthand for writing a method like... more 8/17/2016 12:37:19 PM

people

Weird error when i try to Deserialize a xml string to a .net List Object

I have asked this question twice already but not one have given me an answer that actually works so i will try again. I have this xml: <?xml version="1.0"...
Jon Skeet
people
quotationmark

Your XML file doesn't represent a List<people> - it contains a single people; that's what the root element is. That then contains sub-elements. You can get those easily though: XmlSerializer serializer = new... more 8/16/2016 7:28:02 PM

people

Java: selection between overloaded constructors

Per this question, Java will select the "most specific" option when trying to select between ambiguous overloaded constructors. In this example: public class Test{ private...
Jon Skeet
people
quotationmark

It's not based on the number of types that are convertible to the parameter type - it's whether any value that's valid for one overload is valid for another, due to implicit conversions. For example, there's an implicit conversion from... more 8/16/2016 4:25:03 PM

people

Why does Task.WhenAll return void?

This is the code from the image above: if (claims != null && claims.Any()) { // firstly, why doesn't this work? // var _claimResults = from claim in claims...
Jon Skeet
people
quotationmark

WhenAll returns a Task, but then you're awaiting that task. Awaiting a plain Task (rather than a Task<T>) gives no result. So you either want: Task claimsResult = Task.WhenAll(_claimResults); or await... more 8/16/2016 2:53:22 PM

people

Specific keydown error

Help me I create a KeyDown event here: private void Window_KeyDown(object sender, KeyEventArgs e) { if(e.Key== Keys.Enter) { MessageBox.Show("Enter is...
Jon Skeet
people
quotationmark

I haven't tried this, but it looks like you could just check the modifiers for the associated keyboard device: if (e.Key == Keys.Enter && e.KeyboardDevice.Modifiers == ModifierKeys.None) { ... } more 8/16/2016 7:29:01 AM

people

Cannot call method of type A with parameter type A?

So I don't quite understand this. I'm trying to created a MethodCallExpression, but I'm getting a runtime error stating that the method cannot be called with exactly the type it...
Jon Skeet
people
quotationmark

Look at your method call expression: var matchExpr = Expression.Call(property, matchMethod, propertyExpr, valueExpr); The first argument is the target of the method call - what you're trying to call IsMatch on. Now IsMatch is declared... more 8/16/2016 6:28:40 AM

people

UTC converted to Central Europe Standard Time is 2 hours in front not 1

I'm trying to understand why my date is wrong: DateTime databaseUtcTime = new DateTime(2016, 8, 15, 10, 20, 0, DateTimeKind.Utc); var timeZone =...
Jon Skeet
people
quotationmark

The time zone with an ID of "Central Europe Standard Time" is just the one used by central Europe... it doesn't really mean standard time. As central Europe is observing daylight savings at the moment, the offset really is UTC+2. It's... more 8/15/2016 1:07:54 PM

people

C# use moq to throw exception from async method

I am using the Moq library as a mocking framework together with nunit. I am having trouble to figure out how to setup my mock object to throw an exception from an async method...
Jon Skeet
people
quotationmark

Async methods don't normally throw exceptions directly - they return tasks which end up being faulted. The simplest way to create such a task is to use Task.FromException. You haven't given many details in your question, but I suspect if... more 8/13/2016 2:27:56 PM

people

Converting the date given from Apple receipts to a .net DateTime object

Although it's probably something simple - I couldn't find any place that gives any (correct) solution for it... Apple receipts return dates in RFC 3339 It looks like that: ...
Jon Skeet
people
quotationmark

.NET has no built-in support for the zoneinfo/Olson/tz/IANA time zones (whatever you want to call them...) that are being used here. Fortunately, my Noda Time project does. Here's some code using Noda Time 1.3.2 which successfully parses... more 8/13/2016 1:53:58 PM

people

Is there an equivalent to Java's 'this' inside a static class initializer?

I have a situation where a subclass (let's call it SubClass), during class initialization, calls a static method of the base class (let's call it BaseClass) and passes...
Jon Skeet
people
quotationmark

No, there isn't. Also, as user2357112 said in the comments, the static initializer for BaseClass is only going to run once, while initializing BaseClass. It isn't like an instance constructor, run every time an instance of a subclass is... more 8/13/2016 12:17:08 AM

people