Browsing 7239 questions and answers with Jon Skeet

How to prevent code duplication inside using statement System.Text.Encoding

Ok how can i prevent code duplication in this particular case The duplication happens due to streamreader character encoding. In some cases i want to use default and in some...
Jon Skeet
people
quotationmark

Just use Encoding.UTF8 if you don't have a different one: var encoding = irCustomEncoding == 0 ? Encoding.UTF8 : Encoding.GetEncoding(irCustomEncoding); using (TextReader reader = new... more 9/4/2014 4:27:12 PM

people

Java String encoding

What´s the difference between "hello world".getBytes("UTF-8"); and Charset.forName("UTF-8").encode("hello world").array(); ? The second code produces a byte array with...
Jon Skeet
people
quotationmark

Your second snippet uses ByteBuffer.array(), which just returns the array backing the ByteBuffer. That may well be longer than the content written to the ByteBuffer. Basically, I would use the first approach if you want a byte[] from a... more 9/4/2014 4:19:29 PM

people

Constraint on class type parameters to say types must not be equal?

I wondered if there's any type constraint in C# .NET that states that one type parameter doesn't equal another type parameter; something like this... public class...
Jon Skeet
people
quotationmark

No, there's no such constraint in C# - and I wouldn't expect there to be one in the future, given that it's of fairly limited use. (This isn't a situation that comes up terribly often in my experience.) more 9/3/2014 9:54:39 PM

people

Type is not supported for deserialization of an array

I'm trying to dezerialize an array but I keep running into an error. JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); Profiles thingy =...
Jon Skeet
people
quotationmark

You just need to deserialize it to a collection of some sort - e.g. an array. After all, your JSON does represent an array, not a single item. Short but complete example: using System; using System.IO; using... more 9/3/2014 9:50:45 PM

people

Should I create a new thread as a class that extends Thread or just do a new Thread object instantiation?

I just found out I could create a new thread with a simple Thread myThread = new Thread() { public void run() {} } But most books suggest I make a class that extends...
Jon Skeet
people
quotationmark

I would suggest not extending Thread itself at all. Instead, use the Thread overload which accepts a Runnable: Thread thread = new Thread(new Runnable() { @Override public void run() { ... } }); You're not really... more 9/3/2014 9:29:36 PM

people

What's going on with this method overloading?

I have a question about method overloading in C#. I have a parent class and a child class. class Parent { public virtual string GetMyClassName() { return "I'm...
Jon Skeet
people
quotationmark

Why does str1 get set to "That's a Parent"? Because overloading is (usually) determined at compile-time, not execution time. It's based purely on the compile-time types of the target and arguments, with the exception of calls using... more 9/3/2014 8:53:34 PM

people

Class with nested struct as a property doesn't work

Why doesn't the following code work? If I change h from a property to a field, it works! Or if I change FileHeader from struct to a class it works! I am only looking for an answer...
Jon Skeet
people
quotationmark

This line: o.h.setColNames(); is equivalent to: FileHeader tmp = o.h; tmp.setColNames(); Because FileHeader is a struct, the value of tmp is a copy of the value of the field within o. Modifying tmp doesn't change o.h. I would... more 9/3/2014 6:55:45 PM

people

Why 10.0.64.0/24 overlaps with 10.0.66.0/18?

Why 10.0.64.0/24 overlaps with 10.0.66.0/18? I thought that 10.0.64.0/24 will go up to 10.0.64.255.
Jon Skeet
people
quotationmark

They overlap in that 10.0.64.1 is in both 10.0.64.0/24 and 10.0.66.0/18, for example. It's not the /24 range which is causing you a problem - it's the /18 range, which will include everything from 10.0.64.0 to 10.0.127.255. The "66" in... more 9/3/2014 4:28:44 PM

people

GetHashCode() dummy implementation if not needed

I have already read tons of articles on properly implementing Equals() and GetHashCode(). I have some classes with lots of member variables for which I had to implement an...
Jon Skeet
people
quotationmark

If you really don't want to implement it, I would recommend throwing an exception instead (e.g. NotImplementedException) - then you're not just relying on Debug.Fail working. If you end up in this situation in a production environment,... more 9/3/2014 2:39:04 PM

people

Threads in Java vs Async Tasks in C# Xamarin

I am trying programming in android using Xamarin.android using C#. While watching a tutorial of android I have found out that they are using Thread for asynchronous programming...
Jon Skeet
people
quotationmark

It depends on what sort of asynchronous programming you're talking about. If it's background processing, then you'll still want a task on a different thread. You can still use async/await for that, of course. If it's really just... more 9/3/2014 12:46:50 PM

people