Browsing 7239 questions and answers with Jon Skeet

Compiler not resolving to expected extension method

I noticed today when trying to convert an inline lambda function to a closure so I could use the same lambda in multiple places. This will compile to the correct extension...
Jon Skeet
people
quotationmark

What am I doing here to cause that method to change signatures? A lambda expression doesn't have a type, and is only convertible to compatible delegate and expression-tree types. Therefore the regular IAppBuilder method with (object,... more 1/26/2018 3:42:11 PM

people

Using a lot of IF statements within a FOR loop

Is there a simpler way to complete a task like this where I don't have to use a lot of IF statements. for (int i = 0; i < registration.length(); i++) { char...
Jon Skeet
people
quotationmark

To solve the general case of more elegantly handling multiple if statements which all compare the same value, but to different constants, you'd normally use the switch statement. That would work in this case, but it would still be overkill... more 1/26/2018 1:57:11 PM

people

Is there a more efficient way of reading BLOBs without writing them locally first?

I want to be able to read a document from database and extract some data out of it. I am able to do the second part but I'm having issue with doing it efficiently. Is there a more...
Jon Skeet
people
quotationmark

Part of the reason that it's slow is that you're reading one byte at a time. That's never a good idea. If you're using Java 9, I'd also suggest using InputStream.transferTo(OutputStream) to make things rather simpler: But no, you don't... more 1/26/2018 12:24:37 PM

people

Why does this very simple C# method produce such illogical CIL code?

I've been digging into IL recently, and I noticed some odd behavior of the C# compiler. The following method is a very simple and verifiable application, it will immediately exit...
Jon Skeet
people
quotationmark

The output you've shown is for a debug build. With a release build (or basically with optimizations turned on) the C# compiler generates the same IL you'd have written by hand. I strongly suspect that this is all to make the debugger's... more 1/25/2018 3:01:46 PM

people

How can I force cast a data to the generics specified in the method constraint?

I have the following data class and VM class: public interface IData { string Name { get; } } public class DataPartial: IData { public DataPartial() ...
Jon Skeet
people
quotationmark

It's not the cast that's the problem - it's that you expect the compiler (or runtime) to pick up on the fact that the cast is to a type that declares a new Data property. This line in RunMe: return (T)vmA.Data; ... will always use the... more 1/25/2018 8:33:47 AM

people

Is CS0165 C# compiler error guaranteed for unassigned local variables?

In code like this: int val; if (something()) val = 10; val++; // Error CS0165 Use of unassigned local variable I get CS0165 error message when a local variable is used...
Jon Skeet
people
quotationmark

Is CS0165 guaranteed for such code in C#? Yes, the rules of definite assignment are designed so that a local variable can never be read before it's definitely been written. It's pretty conservative, too - for example: bool... more 1/24/2018 12:38:41 PM

people

Why does my switch work when it has single quotes?

So disclaimer, I am pretty new to C# and trying to learn the finer intricacies. I have a classwork assignment that I coded and it works, but I'm not sure why it works, and want to...
Jon Skeet
people
quotationmark

There's an implicit conversion from char to int, and a constant char expression can be used as a constant int expression, which is what you've got. The value of the int is the UTF-16 code unit associated with the char. Here's another... more 1/24/2018 10:06:07 AM

people

How are anonymous types initialized with LINQ to Entities?

LINQ to entities support anonymous type as return type, such as: var query = from a in b.C select new { Value = a.Value }; and code generated by the compiler will be something...
Jon Skeet
people
quotationmark

If the compiler has to execute an anonymous type initializer, then it ends up calling the constructor. But in LINQ to Entities - or any other IQueryable-based LINQ provider - the code isn't actually executed... it's just converted into an... more 1/24/2018 9:23:16 AM

people

Cannot deserialze the current JSON array into type

I'm trying to deserialize following JSON data : { "bids": [ [ "392031.00000000", "0.00254444" ], [ "390000.00000000", "0.52917503" ], ...
Jon Skeet
people
quotationmark

The JSON you've shown would be represented by: public class OrderBookResponse { [JsonProperty("bids")] public List<List<string>> Bids { get; set; } [JsonProperty("asks")] public List<List<string>>... more 1/23/2018 3:22:44 PM

people

Calculate years, months, days between two dates

I am trying to display the years, months and days between two dates in a C# application that I am creating. (Using console for testing purposes) I'm using NodaTime to achieve...
Jon Skeet
people
quotationmark

The issue is indeed that you're using the "include end date in calculation" in timeanddate.com. The solution isn't to add a day to the period in Noda Time - it's to add a day to the end date before you perform the calculation: Period... more 1/23/2018 2:25:13 PM

people