Browsing 7239 questions and answers with Jon Skeet

Golang time zone parsing not returning the correct zone on ubuntu server

Using Ubuntu 12.04 clientSendTimeHeaderFormat := "2006-01-02T15:04:05-0700" ctx := "2015-04-01T10:04:00-0700" clientSendTime, err := time.Parse(clientSendTimeHeaderFormat,...
Jon Skeet
people
quotationmark

Okay, so I think I know why this is happening - but not a decent fix for it. The documentation for Parse says: When parsing a time with a zone offset like -0700, if the offset corresponds to a time zone used by the current location... more 6/9/2015 7:48:33 PM

people

Why can enum arrays be cast to two different IEnumerable<T> types?

I seemed to have stumbled upon something unusual within C# that I don't fully understand. Say I have the following enum defined: public enum Foo : short { // The values...
Jon Skeet
people
quotationmark

It's not just IEnumerable<T> - you can cast it to the array type as well, so long as you fool the compiler first: public enum Foo : short { A, B } class Test { static void Main() { Foo[] foo = new Foo[10]; ... more 6/9/2015 7:08:52 PM

people

Why the private package String constructor (int, int, char[]) has been removed?

In Java 6, there was a package private constructor to return a new String with the offset being changed. 643 // Package private constructor which shares value array for...
Jon Skeet
people
quotationmark

Yes, String was changed significantly in Java 7 update 6 - now separate String objects never share an underlying char[]. This was definitely a trade-off: Strings no longer need to maintain an offset and length (saving two fields per... more 6/9/2015 6:13:27 PM

people

how to get number of micro seconds in a day

I want to get the number of micro seconds in a day so I tried as per below long microDay = 24 * 60 * 60 * 1000 * 1000; for which I am expecting value as 86400000000 but when I...
Jon Skeet
people
quotationmark

You're performing 32-bit integer arithmetic, as every operand in 24 * 60 * 60 * 1000 * 1000 is an int... but the result is bigger than Integer.MAX_VALUE, so it's overflowing (just as you suspected). (This is actually happening at... more 6/9/2015 5:39:21 PM

people

Why do I get a "Compiler Error Message: CS1002: ; expected" error message at site?

Works fine at my dev. PC, However once I run it on the site I get: Compiler Error Message: CS1002: ; expected byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash,...
Jon Skeet
people
quotationmark

You'll get this if you're using a machine that only has .NET 2.0 or .NET 3.0 installed, and thus only a C# 2 compiler. This code: var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, ... }; ... uses an object initializer... more 6/9/2015 5:37:38 PM

people

LINQ Except/Distinct based on few columns only, to not add duplicates

Excuse me for the confusing title. But I couldn't come up with something more simpler. I have got below class. public class Foo { public FooId int { get; set; } public...
Jon Skeet
people
quotationmark

You either need to override Equals in Foo, or implement an IEqualityComparer<Foo> so that Except can tell when two Foo values are equal. For example: public sealed class FooComparer : IEqualityComparer<Foo> { public bool... more 6/9/2015 9:09:47 AM

people

c# parse json value, check if null

So I have this Web API that calls a service which in turn, returns a json string. The string looks a bit like this: { "title": "Test", "slug": "test", "collection":{ ...
Jon Skeet
people
quotationmark

You're asking for the JToken associated with the configurator key. There is such a token - it's a null token. You can check this with: if (configurator.Type == JTokenType.Null) So if you want to throw if either there's an explicit null... more 6/8/2015 10:15:01 AM

people

Conditionally change GetHashCode() when comparing two objects

I have two different lists of objects and want to get their similarities, based on the weight of some of the properties. Quickest way seems to be with implementing an IEquatable...
Jon Skeet
people
quotationmark

Equals / GetHashCode aren't designed to compare things which are "mostly equal". Equality is just a Boolean property in this case. In particular, having a fuzzy "mostly equal" approach leads to problems with transitivity. The documentaiton... more 6/8/2015 8:12:39 AM

people

Java HashMap, hashCode() equals() how to be consistent with multiple keys?

I have an ID class. It refers to identification for a unique person. There are two forms of ID. Username and user number. If either username matches or user number matches, the...
Jon Skeet
people
quotationmark

I think your question really goes a long way beyond plain implementation of hashCode - it's really about the design of when two IDs should be considered equal. If you have multiple fields all of which are unique, you're actually in a... more 6/8/2015 6:22:57 AM

people

Using members of Var type

After declaring an object of implicit type with var keyword, is it possible to use the members, methods or properties specific to the type that has been assigned to it by compiler...
Jon Skeet
people
quotationmark

Yes, absolute - because var isn't actually a type. It just tells the compiler: "I'm not going to explicitly tell you the type - just use the compile-time type of the right-hand operand of the assignment operator to work out what type the... more 6/7/2015 11:36:23 AM

people