Browsing 7239 questions and answers with Jon Skeet

Issue on converting time from local to UTC at DST end date while using TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo) method

I have an application that converts local time to UTC and stores it in the database. I encountered this problem while I was testing the conversion during a particular date - 1st...
Jon Skeet
people
quotationmark

Isn't this wrong? why did the converted time increase by an hour on 1st November? Because that's when the clocks change, as you say. The problem is that "2015-11-01 01:49:00.000" is ambiguous in Pacific Time - it occurs twice, once... more 6/18/2015 7:55:02 AM

people

ForEachAsyc with Result

I'm trying to change Stephen Toub's ForEachAsync<T> extension method into an extension which returns a result... Stephen's extension: public static Task...
Jon Skeet
people
quotationmark

Your LINQ query can only ever have the same number of results as the number of partitions - you're just projecting each partition into a single result. If you don't care about the order, you just need to assemble the results of each... more 6/18/2015 6:21:11 AM

people

List of type T to Dictionary

I have an object List<T> and a keyfieldName of Type T i am trying to convert this List<T> to an Dictionary<string,double> for example if Type T is of type...
Jon Skeet
people
quotationmark

If you know the type in advance, I'd just use: var dictionary = list.Select(x => x.FirstName + ":" + x.UserId) .Concat(list.Select(x => x.LastName + ":" + x.UserId)) .ToDictionary(p => p,... more 6/18/2015 5:48:20 AM

people

Initialize method from HashSet

I have added the element spells.add(new Magic("boring",2,true)); and similar, but when I try to run for (Magic set : spells) { Magic magic1 = new Magic(spells); ...
Jon Skeet
people
quotationmark

Why are you constructing a new Magic instance within the loop (or trying to, anyway)? Surely you just need the ones in the set: for (Magic spell : spells) { spell.go(); } Note that currently you're ignoring the set variable in your... more 6/17/2015 9:30:39 PM

people

Linq to XML select descendent of descendant each with specific attribute

I have found many articles on getting a descendant with a specific attribute, but I can't seem to find anything on selecting multiple descendants with different attributes with...
Jon Skeet
people
quotationmark

There are multiple options here, but I'd suggest the simplest thing is just to check each Grandchild: var grandchildren = doc .Descendants("Grandchild") .Where(x => (string) x.Parent.Parent.Attribute("name") == "Ken"... more 6/17/2015 8:13:19 PM

people

Static string variable with complex initialization in C#

I have a class with a static string variable that has a somewhat complex initialization (I can't just set it equal to a quoted string like "whatever"). I need to run a few lines...
Jon Skeet
people
quotationmark

You can just call a method: private static readonly string _myString = GetMyStringValue(); private static string GetMyStringValue() { MyObject obj = new MyObject(); obj.someSetupHere(); return obj.ToString(); } You could do... more 6/17/2015 8:08:00 PM

people

Buffer.BlockCopy does not work on double array?

the following code double[] src = new[] { 1d, 1d, 2d }; double[] dst = new[] { 0d, 0d, 0d }; Buffer.BlockCopy(src, 0, dst, 0, 3); for (int i = 0; i < 3; i++) { ...
Jon Skeet
people
quotationmark

You're telling BlockCopy to copy 3 bytes. You want Buffer.BlockCopy(src, 0, dst, 0, 24); or Buffer.BlockCopy(src, 0, dst, 0, 3 * sizeof(double)); ... in order to copy all 24 bytes. The fifth parameter of Buffer.BlockCopy is... more 6/17/2015 8:04:29 PM

people

Method declared with dynamic input parameter and object as return type in fact returns dynamic

namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var objectGetter = new ObjectGetter(); var obj = objectGetter.GetItem();...
Jon Skeet
people
quotationmark

The problem is that you're calling a method with a dynamic argument. That means it's bound dynamically, and the return type is deemed to be dynamic. All you need to do is not do that: object dObj = "123"; var obj = Convert(dObj); Then... more 6/17/2015 5:35:09 PM

people

Delegate caching behavior changes in Roslyn

Given the following code: public class C { public void M() { var x = 5; Action<int> action = y => Console.WriteLine(y); } } Using VS2013,...
Jon Skeet
people
quotationmark

Still I wonder, what are the benefits of lifting the delegate into a new class and caching it there over simply caching it at the call site? You've missed one other really important detail - it's now an instance method. I believe... more 6/17/2015 4:50:52 PM

people

IllegalBlockSizeException when using aes/ecb/pkcs5padding to decrypt a byte array

I understand that aes encryption needs to be in blocks of 16, but I was under the impression that using Cipher.getInstance("AES/ECB/PKCS5PADDING"); padded the byte array to...
Jon Skeet
people
quotationmark

This is the problem: toDecrypt = new byte[fis.available()+1]; Firstly, you're using the available() method, which is never a good idea. Next, even assuming it is returning the length of the file, you're adding 1 to it - why? You want... more 6/17/2015 4:37:41 PM

people