Browsing 7239 questions and answers with Jon Skeet

C#: Assign array to another array: copy or pointer exchange?

Sorry for asking this question, I have been Googling a bit but it seems what comes up is references to clone or copy methods, not an actual answer for my question in C#. I have...
Jon Skeet
people
quotationmark

Assignment always just copies the value of one expression into a variable (or calls a property/indexer setter). In your case, with this: buffer1 = buffer2; ... the value of buffer2 is just a reference to a byte array. So after that... more 4/1/2015 7:12:31 PM

people

Unexpected behavior for a collection of an instance

For example i have the following class: class Person { public List<int> Grades{ get; set; } public Person(List<int> grades) { this.Grades =...
Jon Skeet
people
quotationmark

I expected that the collection "saved" into Person instance to stay how it was initialized. It's time to revisit your expectations of how reference types work in C# then :) I have an article that you might find useful... What... more 4/1/2015 7:04:52 PM

people

Getting all objects with same key from a MultiValueMap

I have a MultiValueMap<Integer, Path> from which I am trying to get [print for the purpose of this question] out all the paths which were put in the map using the same...
Jon Skeet
people
quotationmark

The entry set seems to be declared with an unfortunate signature. But you could iterate over the keys instead, and call getCollection for each: for (Integer key : duplicates.keySet()) { Collection<Path> paths =... more 4/1/2015 6:57:45 PM

people

Weird behaviour of compareTo(GregorianCalendar c)

Could you tell me why the fillowing code: int a = new GregorianCalendar(2015,3,31,7,45).compareTo( new GregorianCalendar(2015,4,1,7,45); System.out.println(a); prints...
Jon Skeet
people
quotationmark

You're comparing "April 31st" with May 1st. There is no April 31st, so it's rolling over to May 1st anyway. (Okay, it would make more sense to just throw an exception, but hey... that's far from the worst piece of API design in... more 4/1/2015 3:42:42 PM

people

Maintainable Conversions

I have a class that inherits from a base class; it is almost exactly the same, the difference is a TypeParam to help with intellisense. I want to provide a way to convert an...
Jon Skeet
people
quotationmark

The typical way is to provide a copy constructor within each class in the hierarchy. For example: class A { private int f1; private int f2; public A() { ... } public A(A original) { f1 =... more 4/1/2015 3:38:01 PM

people

optional parameter: reversed precedence if override of overload exists

I wonder about the following behavior: public class A { public virtual void Do() { Console.WriteLine("A"); } } public class B : A { public override void Do() {...
Jon Skeet
people
quotationmark

Why an overload with exact signature doesn't have precedence over another overload with optional parameters in case that the overload with exact parameters overrides a base implementation? Basically this is the compiler following the... more 4/1/2015 1:48:22 PM

people

Extract files from *.gz extension

I have successfully extracted *.gz from *.tgz and now I have no idea how to actually extract final files from *.tgz. There are some options using custom packages but that's not...
Jon Skeet
people
quotationmark

A .tgz file wouldn't normally be extracted to a .gz file - it would be extracted to a .tar file. (A .gz file is gzipped; a .tar file is an uncompressed archive containing multiple files; a .tgz is a .tar file that's then been gzipped -... more 4/1/2015 12:15:17 PM

people

Iterating through enum with EnumMember attribute

Given the following enum: public enum JobTypes { [EnumMember (Value = "IN PROGRESS")] IN_PROGRESS, SUBMITTED, [EnumMember (Value = "IN REVIEW")] ...
Jon Skeet
people
quotationmark

EnumMemberNameAttribute only affects serialization: The EnumMemberAttribute enables fine control of the names of the enumerations as they are serialized. It doesn't have any effect on the result of calling ToString() on the value,... more 4/1/2015 4:48:04 AM

people

Create a empty array of points

I'm trying to create a empty array of points, for this i'm using the following Point[] listapontos = new[]; //should create a empty point array for later use but doesn't. ...
Jon Skeet
people
quotationmark

// should create a empty point array for later use but doesn't. No, what you've specified just isn't valid syntax. If you want an empty array, you could use any of: Point[] listapontos = new Point[0]; Point[] listapontos = new... more 4/1/2015 4:22:34 AM

people

Probability and Random number generator

I'm working on a problem that involves probability and random number generator that I believe I am close with but need help hammering out one last thing. I have a marble bag in...
Jon Skeet
people
quotationmark

Personally, I wouldn't use double at all - I'd just pick a random integer between 0 (inclusive) and the total number of marbles (exclusive). Effectively, you'd be "labelling" each marble with a number, and then working out which marble was... more 4/1/2015 12:57:58 AM

people