Browsing 16016 questions and answers with Jon Skeet

Javascript New Date() returns invalid date

I have an Array with date strings formatted: "24-Jul-2017". and when i use new Date("24-Jul-2017"); it returns date with one day offset. 2017-07-23T22:00:00.000Z Tried different...
Jon Skeet
people
quotationmark

Assuming the browser is in a time zone with a UTC offset of +2 at the start of July 24th 2017, it's behaving as documented. The Date constructor is documented as behaving like Date.parse, which then includes this documentation - along... more 10/26/2017 9:04:54 PM

people

Why does a lambda expression in C# cause a memory leak?

Note: this is not just some random useless code, this is an attempt to reproduce an issue with lambda expressions and memory leaks in C#. Examine the following program in C#....
Jon Skeet
people
quotationmark

I suspect that what you're seeing is the effect of a compiler optimization. Suppose Test() is called multiple times. The compiler could create a new delegate each time - but that seems a little wasteful. The lambda expression doesn't... more 10/26/2017 8:22:15 PM

people

Why value type fields may be instantiated, instead of initialized?

A value type local variable (e.g. S s; given struct S{}) is initialized though the invocation of its constructor (e.g. a = new S(11)), if S declares a constructor with int...
Jon Skeet
people
quotationmark

Basically, this behaviour is required in order to separate the constructor call from the assignment. The expected observable behaviour is that if a constructor throws an exception, the assignment doesn't take place. That won't be the case... more 10/25/2017 4:09:32 PM

people

C# Cannot Deserialize Complex Json Object With Array

So I use HttpClient() to get Json object from web api. The object contains the list of all provinces in a country. This code calls the json deserialization: rj =...
Jon Skeet
people
quotationmark

Currently your Results property claims to be an object containing an array - as if your JSON for it looked like this: "results": { states: [{ "province_id": "1", "province": "Bali" }, ... more 10/25/2017 5:25:08 AM

people

Queue for more producer (thread) single consumer (thread)

I'm trying to find solution for more threads writing to a queue and a single thred is reading from it. I found ConcurrentQueue it has TryDequeue(T) that is thread safe but I'm...
Jon Skeet
people
quotationmark

Yes, the whole point of the concurrent collections is that they're thread-safe. It's fine to write to ConcurrentQueue<T> from multiple threads. From the documentation: All public and protected members of ConcurrentQueue<T>... more 10/24/2017 9:53:52 PM

people

how to close File Stream in C#

Please see this code. string name = dt.Rows[0]["Name"].ToString(); byte[] documentBytes = (byte[])dt.Rows[0]["DocumentContent"]; int readBytes = 0; //int index = 0; readBytes...
Jon Skeet
people
quotationmark

You're asking Excel to open the file while you still have the stream open. Given that you're just trying to write bytes to it, I'd just use: // This will close the file handle after writing the data File.WriteAllBytes(name,... more 10/24/2017 2:41:10 PM

people

NodaPatternConverter for Instant with numeric (unix) format in 2.x.x

As I can read on https://nodatime.org/2.0.x/userguide/migration-to-2 the support for numeric formatting of Instants has been removed. Is there currently a way to create a...
Jon Skeet
people
quotationmark

Well, you can implement IPattern<T> yourself. Your parser would just need to use long.Parse then call Instant.FromUnixTicks. The formatter would just need to call Instant.ToUnixTimeTicks and format the result. Ideally, do both of... more 10/24/2017 6:51:25 AM

people

Why string.Format does not throw ArgumentNullException?

According to MSDN String.Format throws if format is null (pretty reasonable), link here. But testing says it only does that if the second argument is null as well, not if the...
Jon Skeet
people
quotationmark

Ah, the joys of overload resolution. In that case, you're actually calling string.Format(IFormatProvider, string, params object[]) - so you're passing a null argument for the provider parameter, which is entirely valid (and means to use... more 10/23/2017 4:39:04 PM

people

C# ToDictionary get Anonymous Type value before C # 7

Hello here is how i get value from dictionary myage value after C# 7 static void Main(string[] args) { List<User> userlist = new List<User>(); ...
Jon Skeet
people
quotationmark

Three options: First, you could write an extension method like this: public static TValue GetValueOrDefault<TKey, TValue>( this IDictionary<TKey, TValue> dictionary, TKey key) { TValue value; ... more 10/23/2017 2:44:58 PM

people

Difference between 1/0 and 1.0/0.0 in java

I am new in Java programming language, a stupid question comes up as below. to execute 1/0 in java will be generated a runtime exception, as the value is undefined. but the...
Jon Skeet
people
quotationmark

anything special with double data type? Yes, double can represent infinity (as can float), whereas int can't. The floating point types follow IEEE 754, which allows for signed infinity values, as well as "not a number" (NaN) values,... more 10/23/2017 9:50:51 AM

people