Browsing 7239 questions and answers with Jon Skeet

HashMap is creating many instances of itself inside itself

As you can see the hashmap is creating many instances of itself and I am not sure why, this is the code for the class which I am using, private Map<String, Integer>...
Jon Skeet
people
quotationmark

You're just confusing yourself in the debugger, basically. A HashMap has an entry set - and that entry set refers to the containing map. All you're doing in the debugger is navigating from the map to the entry set, back to the map, back... more 2/23/2016 10:40:02 AM

people

Code not working properly datareader from database in C#

Hi I got an InvalidCastException saying that "Specified cast is not valid". I don't know where is the problem. Does my code has an error? Wordlist column is text field. This is...
Jon Skeet
people
quotationmark

Basically, you're not using the reader properly. DbDataReader.Read() returns a Boolean value indicating whether or not it's managed to need another row of results. You're currently treating it as if it returns the next result, returning... more 2/23/2016 6:48:28 AM

people

Dot notation for class member access

I've got the following code: public class Random { public string Name { get; set; } public bool IsRunning() { var running = true; return running; ...
Jon Skeet
people
quotationmark

You're trying to write code directly within the class declaration. A class declaration can only directly contain member declarations. It can't contain arbitrary statements such as newObject.Name = "Johnny" nor can it use var, which is only... more 2/22/2016 4:02:04 PM

people

Json.NET Preserve double/decimal datatypes

I have reduced our real-world case to a simpler one (which may now look a bit constructed, yet hopefully easier to comprehend). Basically we have an loosely typed array object[]...
Jon Skeet
people
quotationmark

Your preferred format simply isn't JSON. Values in JSON are only: Numbers (no differentiation between integers and non-integers) Strings Objects Arrays Boolean true/false null That's it. So the only way of preserving extra information... more 2/22/2016 2:08:16 PM

people

Throw new exception using object initializer

Let's consider this code : try { return new ClassA.GetStuff(); } catch (Exception e) { throw new MyException ("message", e) {SomeMyExceptionProperty =...
Jon Skeet
people
quotationmark

As ever, the official documentation is the C# specification. The important part is that this is just a throw statement. It has two parts (in this case): The keyword throw An expression which determines the exception to throw In this... more 2/22/2016 10:34:48 AM

people

I'm getting irregular output for this program? Why?

Finally example public class FinallyExample { public static void main(String[] args) { new FinallyExample().dothework(); } public void dothework() { ...
Jon Skeet
people
quotationmark

Basically, you're writing to System.out and System.err. It's unclear (and implementation-specific) exactly how much data those will buffer, or when they will be flushed, but you shouldn't necessarily expect that to be consistent every... more 2/22/2016 7:05:49 AM

people

C# StreamWriter Is Creating My File But No Content

I'm trying to use StreamWriter to log a search every time someone searches on my program. I can get StreamWriter to write a new file but it does not create any content. I've tried...
Jon Skeet
people
quotationmark

You haven't closed the writer. The writer buffers data, so if you just let it get garbage collected without flushing or closing it, the file handle will be closed without ever writing the data to the file. You should ideally do this with... more 2/21/2016 5:16:01 PM

people

What C# datatype should be used for a partially constant set of information?

I'm writing a C# application that does some things with baseball statistics. I'm getting the data from an XML web service. I want to completely separate the...
Jon Skeet
people
quotationmark

It sounds like you need one class for "a statistic" - and then a Dictionary<PlayerStat, int> (or whatever the value would be). The PlayerStat class would know about the abbreviation, description and XML attribute name - and I'd... more 2/21/2016 7:55:07 AM

people

How to create byte[] with size using reflections?

Introduction: So, I'm using an API and I need to overwrite 2 100% identical functions, one returning byte[] and one returning short[]. To make sure I don't have to edit everything...
Jon Skeet
people
quotationmark

Just use java.lang.reflect.Array.newInstance: Object byteArray = Array.newInstance(byte.class, 4096) Object shortArray = Array.newInstance(short.class, 4096); You can use the other methods in Array to manipulate the array. more 2/20/2016 12:14:26 PM

people

How to avoid repetition with multiple overrides of method accepting Func<T, int> as parameters

I would like to have a "generic" - that is, a method with a single signature if possible - instead of this rather copy/paste code below. My goal is to "wrap" methods from a third...
Jon Skeet
people
quotationmark

Well you could start off by making them generic: public TResult Spy<TResult>(Func<TResult> method) { methodCalls.Add(method.Method.Name); return method(); } public TResult Spy<TArg, TResult>(Func<TArg,... more 2/19/2016 6:30:12 PM

people