Browsing 7239 questions and answers with Jon Skeet

Maintaining order in HashMap

I have a list which I convert to a map to do some work. After that, i convert the map back again to a list, but this time the order is random. I need the same initial order...
Jon Skeet
people
quotationmark

HashMap itself doesn't maintain insertion order - but LinkedHashMap does, so use that instead. As documented... HashMap: This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order... more 10/3/2013 4:45:02 PM

people

Can you set/update a value in real time within a LINQ statement during iteration?

Based on a proposed answer to my other question here... is it possible to update a variable during LINQ enumeration so you can use it as part of a test? For instance, is anything...
Jon Skeet
people
quotationmark

For LINQ to Objects (which takes delegates) then you can, yes - using a statement lambda: sourceList.Reverse() .TakeWhile(o => { ... fairly arbitrary code here return someValue; }) ... more 10/3/2013 4:44:01 PM

people

parsing a string value to double with comma decimal seperator

if there is any one who find a solution to this string x = "7,50"; string y = "5"; double a = double.Parse(x); double b = double.Parse(y); double c = a - b; then the result...
Jon Skeet
people
quotationmark

Just specify the appropriate culture to double.Parse. For example: CultureInfo french = new CultureInfo("fr-FR"); double x = double.Parse("7,50", french); I suspect you actually had "7,5" as a value, however - as "7,50" would be parsed... more 10/3/2013 4:29:04 PM

people

Why doesn't catching Exception catch RuntimeException?

This is very odd to me. RuntimeException inherits from Exception, which inherits from Throwable. catch(Exception exc) { /* won't catch RuntimeException */ but catch(Throwable...
Jon Skeet
people
quotationmark

The premise of the question is flawed, because catching Exception does catch RuntimeException. Demo code: public class Test { public static void main(String[] args) { try { throw new RuntimeException("Bang"); ... more 10/3/2013 4:13:40 PM

people

Copying from an InputStream to a BufferedOutputStream

I am sending a file over a socket and am trying to write it to a file at the receiver. I've seen examples that do something like this: int bytesRead = 0; while (bytesRead !=...
Jon Skeet
people
quotationmark

My typical loop for this sort of thing looks like this: int bytesRead; while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } While I don't usually like performing an assignment within a condition... more 10/3/2013 3:53:10 PM

people

C# Closure is not working as expected

I can't understand quite clearly the difference between two blocks of code. Consider there is a program class Program { static void Main(string[] args) { ...
Jon Skeet
people
quotationmark

These two blocks of code should work identically. No they shouldn't - at least in C# 5. In C# 3 and 4 they would, in fact. But in the foreach loop, in C# 5, you have one variable per iteration of the loop. Your lambda expression... more 10/3/2013 2:26:05 PM

people

ZipException: error in opening zip file

I'm working on a method which will take a zipped file, unzip it, and return a new file/directory containing all the unzipped files. The goal is to then take that directory and...
Jon Skeet
people
quotationmark

Okay, I now believe that this is the problem: ZipInputStream zis = new ZipInputStream(in); out = new FileOutputStream(entryDestination); out.write(zis.read()); out.flush(); out.close(); You're creating a new file, and writing a single... more 10/3/2013 2:19:21 PM

people

Public members in package private class

I wonder if it's okay (not considered bad practice) to have public members in package private class. I tend to add public keyword to members of my default visibility classes to...
Jon Skeet
people
quotationmark

I do it only for readability, since in this case public members have essentially the same visibility as members without any access modifiers (i.e. package visibility). Is that correct? Well that depends. Not if you're overriding... more 10/3/2013 2:12:08 PM

people

non Static Implicit Operator

Does anyone have an eloquent solution for the lack of support of non-static implicit operators in C#? The following codes shows my current problem: class Foo { ...
Jon Skeet
people
quotationmark

Fortunately, you can't do this. I say "fortunately" because it's very confusing for the reader. I suggest you just write a method instead, e.g. MergeFrom, so that you're code then reads: // Object initializers used for readability. Foo... more 10/3/2013 2:06:49 PM

people

Calling a function using a parameter in it's name

I've got a bit of strange question, I'll show you my code first, and then I'll ask the question. I have this switch function: switch(index) { case 1: ...
Jon Skeet
people
quotationmark

Not like that, no. You could use delegates: // Ideally make this a readonly field. Action[] actions = { Level1, Level2, Level3, Level4, Level5 }; ... actions[index - 1](); Or you could use reflection, as others have mentioned... but I'd... more 10/3/2013 12:31:47 PM

people