Browsing 7239 questions and answers with Jon Skeet

Calendar giving unexpected results for year 1

I tried to do this code-golf challenge in Java 7. Just for anyone that doesn't know: code-golf is to complete a certain task in as few bytes as possible. Obviously Java isn't a...
Jon Skeet
people
quotationmark

Unfortunately, GregorianCalendar is badly named. It's actually "Julian / Gregorian calendar, with a switch-over between them." Fortunately, you can use it to act as a pure Gregorian calendar by setting that switch-over to be at the start... more 10/27/2016 8:56:17 AM

people

Using 'Default' keyword on an instance of type 'Object'

When supplied with an integer (or other value type) boxed as a object, I'm pretty sure there is no way to use default() (returns the default value of a given type) on it directly...
Jon Skeet
people
quotationmark

Assuming you can't change the method to be generic, you can just use the fact that all value types provide a parameterless constructor, so you can call Activator.CreateInstance: public object GetDefaultValue(object obj) { Type type =... more 10/27/2016 8:40:15 AM

people

BinaryReader 30x faster after first run. Byte array still in memory?

My program reads x bytes from a file, checks if they are all zeros, repeats the process for 20.000 files, and keeps a list of the files that have non-zero bytes. Trying to monitor...
Jon Skeet
people
quotationmark

This almost certainly has nothing to do with anything .NET is doing - it'll be the file system transparently caching for you. To test this, change your code to just use FileStream and simply loop over the file reading it to a buffer and... more 10/26/2016 2:52:48 PM

people

Sharing a DLL between projects

Microsoft says it's platform neutral these days, so I'm trying to build on Mac and Linux only with VS Code and deploy to Azure. Why? It's mainly to prove that I can. Our project...
Jon Skeet
people
quotationmark

So instead of copying the DLL, use dotnet pack to build a nuget package on your Mac, and copy that to your Linux machine instead. Put it in a directory that you configure as a local nuget repository (no server or anything required) and... more 10/25/2016 6:08:54 PM

people

Linq expression meaning

if (regionTerritory.Any(m => m.Region == int.Parse(region.RegionCode))) { // region exists, add territory regionTerritory.First(m => m.Region ==...
Jon Skeet
people
quotationmark

m is the parameter to the lambda expression. It will be called with each element in the collection (as far as necessary). This is an inefficient way of writing the code, however. It would be better to use: int regionCode =... more 10/25/2016 5:48:21 AM

people

Noda Time Create a ZonedDateTime from DateTime and TimeZoneId

Let's say I have the following date, time, and time zone: 2016-10-15, 1:00:00, America/Toronto. How do I create a ZonedDateTime that represents that exact date and time in the...
Jon Skeet
people
quotationmark

What you've described is precisely the behaviour of LocalDateTime.InZoneLeniently in Noda Time 2.0. (Thanks to Matt Johnson's change :) However, as that's still in alpha, here's a solution for 1.3.2. Basically, you just want an appropriate... more 10/24/2016 7:03:02 PM

people

Get all the attribute values by attribute name from XML file using Linq to XML

I have an XML file and I have to extract all the attribute values from XML. I have tried the below one but I need it in Linq. Can anyone guide me how to do this. Sample...
Jon Skeet
people
quotationmark

Okay, so it looks like you want something like this, which loads all the field elements in the Import just-below-root element, then loads the reference lists by finding every element which isn't Import. var doc =... more 10/24/2016 7:20:22 AM

people

Whats difference between this two: BigInteger.valueOf(10000) and BigInteger.valueOf(0010000)?

I was working with one problem and came across this. What happen is: when we use this: BigInteger.valueOf(10000) it gives value of 10000 But when we use this...
Jon Skeet
people
quotationmark

0010000 is an octal literal. This has nothing to do with BigInteger - it's just Java integer literals (JLS 3.10.1): System.out.println(10000); // 10000 System.out.println(0010000); // 4096 From the JLS: A decimal numeral is either... more 10/24/2016 6:48:50 AM

people

Generic this index property in dictionary

How can I achieve something like this. I don't want method that return casted value, I want use this[] is this possible? public class DynamicDictionary : IDictionary<string,...
Jon Skeet
people
quotationmark

No, you can't. Indexers - and properties in general - can't be generic in C#. You'd have to use a method instead, or just cast in the calling code. more 10/22/2016 8:07:55 AM

people

Repeat same method and using new value of one parameter

I'm trapped within this certain predicament of mine and I would gladly accept any suggestion. So here it is: I'm currently working in a method where a certain variable is...
Jon Skeet
people
quotationmark

One simple option is to have a private overload taking the server parameter - then you can call it recursively: public void myMethod(String args[]) { myMethod(args, "some value"); } private void myMethod(String[] args, String server)... more 10/21/2016 5:54:16 AM

people