Browsing 7239 questions and answers with Jon Skeet

C# BinaryReader and Little Endian

I have to read a binary file so I use this code: static void ReadBin() { var sectors = new List<Sector>(); using (var b = new...
Jon Skeet
people
quotationmark

The endianness of BinaryReader only affects methods such as ReadInt32. It doesn't affect ReadBytes - that will always return you the bytes in the order in which they occur within the stream. If you need a byte array (or a hex... more 9/26/2014 2:01:30 PM

people

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List

I am new to LINQ I am getting this error Cannot implicitly convert type System.Collections.Generic.List<AnonymousType#1> to ...
Jon Skeet
people
quotationmark

Your select clause: select new { ... } is selecting an anonymous type. You need it to select a DssClient_Sasid_Certified, so that you can return the type that your method declaration says you're going to return. You may just need... more 9/26/2014 12:43:07 PM

people

Why does #warning in VS2013 is producing an error?

I am using Visual Studio 2013, with Resharper 8.2. I must be missing something here. I have added #warning to the code and it failed the build. Question is why? isn't that's...
Jon Skeet
people
quotationmark

I suspect you have set the "treat warnings as errors" flag in your project build properties. You can either remove that flag, or set it to only treat specific warnings as errors. more 9/26/2014 12:40:09 PM

people

Linq to Sql query, datediff + 3 days

I'm working with a flights page. Right now I got some flights in my database and I want to display them. With this query I'm displaying all flights on the selected date...
Jon Skeet
people
quotationmark

All you need is to work out the start and end dates, then use comparisons in your where clause: var earliest = depDate.AddDays(-3); var latest = depDate.AddDays(4); // Exclusive var destinationFlights = ... where ... &&... more 9/26/2014 10:23:54 AM

people

Why does List.Count() and IEnumerable.Count() return different results?

We have this array in LINQ. string[] colors = { "green", "brown", "blue", "red" }; and this code return 0. var list = new List<string>(colors); IEnumerable<string>...
Jon Skeet
people
quotationmark

The Where method doesn't execute your query - it just builds a query, which is then executed when the data is requested. This is known as deferred execution. So in your first snippet, the filter is only applied when Count() is called -... more 9/26/2014 10:01:27 AM

people

Why is my C# if statement code not working?

I can't figure out why this If statement doesn't work. if (textBox2.Text.Contains(".xwm") && textBox4.Text.Contains(".xwm") == true) { textBox4.Text.Replace(".xwm",...
Jon Skeet
people
quotationmark

You're calling Replace on a string, but then not doing anything with the result. Strings are immutable in C# - any methods which sound like they might be changing the string actually just return a reference to a new string (or potentially... more 9/26/2014 5:30:26 AM

people

Sanity check, using Math.Round to round up to two decimal places

Out of curiosity why does the following test fail? Doesn't Math.Round, round up? [Test] public void MathRound() { Assert.AreEqual(7.13, Math.Round(7.125,2)); ...
Jon Skeet
people
quotationmark

From the Round documentation (looking at Round(double, int): Rounds a double-precision floating-point value to a specified number of fractional digits, and rounds midpoint values to the nearest even number (example). Your value is... more 9/25/2014 9:40:42 PM

people

StringEscapeUtils.escapeJava Error Cannot escape double quotes

I have a string which looks like: text = " "12.10 On-Going Submission of ""Made Up"" Samples." " I am trying to escape the double quotes. I have tried the following...
Jon Skeet
people
quotationmark

Currently you're just importing StringEscapeUtils in a regular way, but then you're not explicitly calling the method from the right class. I suspect you either want: import static groovy.json.StringEscapeUtils.escapeJava; textFinal:... more 9/25/2014 7:04:53 PM

people

Get all the direct Children of a specified Parent

I have a method called GetMenuItems which returns Heirarchical results. Here is the implementation: public static ObservableCollection<MenuItem> GetMenuItems() { ...
Jon Skeet
people
quotationmark

You should split your task into two parts: Finding the menu with a particular ID Getting its children The latter is simple - you just use the menuItem property, which would be better named Children or something similar. I'd tackle the... more 9/25/2014 6:53:12 PM

people

Is the string == overloaded operator actually used in some generic comparison method?

I am currently reading Jon Skeet's C# in Depth, 2nd Edition. I'd like to quote listing 3.5: static bool AreReferencesEqual<T>(T first, T second) where T : class { ...
Jon Skeet
people
quotationmark

Yes, there's a massive difference. In example 1, intro1 and intro2 refer to different objects. In examples 2 and 3, intro1 and intro2 have the same value - they refer to the same object, so if you call Object.ReferenceEquals(intro1,... more 9/25/2014 6:14:53 PM

people