Browsing 7239 questions and answers with Jon Skeet

Difference between default(int?) vs (int?)null

Is there any difference using default(int?) or (int?)null to assign a variable? Is the same thing? Or exists some pros and cons to use each way?
Jon Skeet
people
quotationmark

They're exactly the same thing, as is new int?(). If you're just assigning a variable, you normally wouldn't need it at all though. I'd just use: int? x = null; for example. The time I most often need one of these expressions is the... more 11/4/2016 5:31:30 PM

people

How Linq's GroupBy method has a deferred execution?

I've found this question, but it has no answer yet... What algorithm does Linq GroupBy use? Since you have to iterate over the entire source collection to know all the groups,...
Jon Skeet
people
quotationmark

(I'm assuming we're only talking about LINQ to Objects.) It's still deferred in that until you start asking for results, it won't read the source collection at all. But yes, once you ask for the first result, it will indeed read the whole... more 11/4/2016 3:49:59 PM

people

Unable to create a constant value of type. While trying to match data from viewmodel to context

Im trying to get selected devices from view and match them with the devices in context but i only get the following: Unable to create a constant value of type...
Jon Skeet
people
quotationmark

I suspect you should make selected a list of IDs... then you can just use Contains: public ActionResult TransferDevices(IList<XXXViewModel> viewModel) { var selected = viewModel.Where(x => x.isSelected) ... more 11/4/2016 9:29:42 AM

people

Reason for KeyNotFoundException in Dictionary initialization

The following code new Dictionary<string, List<int>> { ["a"] = {1}, }; Throws a run-time KeyNotFoundException, albeit that {1} is a perfectly well-formed array...
Jon Skeet
people
quotationmark

Your first piece of code is using a collection initializer, which doesn't use logical assignment, but instead is intended to call Add on an existing collection. In other words, this: var x = new Dictionary<string, List<int>>... more 11/3/2016 9:31:57 AM

people

Why does c# compiler leave il code for conditional method?

I have following program: public class TestClass { [Conditional("DEBUG")] static void debug_foo() { Console.WriteLine("DEBUG foo"); } static int...
Jon Skeet
people
quotationmark

The method logically still exists, and could be called by reflection, for example. The C# 5 specification only talks about the calls being omitted - nothing about the implementation being omitted (section 17.4.2): A method decorated... more 11/3/2016 9:29:15 AM

people

Throw error. How can I fix this? I've tried many things, but I am new to it and the logic is a little beyond me

The line with "private static BufferedReader" is where the problem lies. "-unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown" is the...
Jon Skeet
people
quotationmark

The problem is more simply demonstrated like this: import java.io.*; class Test { private static Reader reader = new FileReader("foo.txt"); } The problem is that the static initializer for your class can throw an exception. That's... more 11/3/2016 7:54:17 AM

people

.net invalid characters when converting string to xml

I'm trying to convert web response to xml. However, even if xml string is validated, i'm getting "invalid character error" when i try to convert response string to XmlDocument. I...
Jon Skeet
people
quotationmark

You're using XmlDocument.Load, which is meant to accept a URL. I believe you meant to use XmlDocument.LoadXml which parses the text as XML. (As an aside, I'd strongly recommend updating to use XDocument if you possibly can. LINQ to XML is... more 11/3/2016 7:44:07 AM

people

Conditional Flags Assembly cmp, test

I'm having a hard time understanding conditional code in assembly. The assembly on the right is for funA() on the left, but I'm having trouble with lines 3-4 in assembly. Here...
Jon Skeet
people
quotationmark

If you look at .L1 and .L2, the JIT compiler has just decided to reverse the order - it's put the else code first, and reversed the condition. The jle is "jump if less than or equal" so it's become the equivalent of the C#: if (a[idx]... more 11/2/2016 6:40:19 PM

people

HashSet not removing all duplicate entries

I am attempting to use a HashSet to make sure data I read in from a .txt file are unique. Below is the sample data; 999990 bummer 999990 bummer 999990 bummer 999990 ...
Jon Skeet
people
quotationmark

I would like any new entry with either the same weight or term as previously added entries to be considered a duplicate. That's not how equality works. Equality has to be transitive - so if x.equals(y) returns true, and y.equals(z)... more 11/2/2016 2:17:58 PM

people

Is NodaTime compatible with NetCoreApp 1.0?

I have dotnet core app that I'm working on that is cross-platform. I wanted to use Noda as a utility in my project but get the following error even tho I have nodatime defined as...
Jon Skeet
people
quotationmark

Noda Time 1.x only targets PCLs. You may be able to get it to work with netcoreapp1.0, but I'm not going to guarantee it. But Noda Time 2.0 targets netstandard1.1, so should be fine. That's only available as an alpha release right now,... more 10/31/2016 6:12:36 PM

people