Browsing 7239 questions and answers with Jon Skeet

Correct ways to manipulate / get information from array of objects

I have defined an array of objects for a class Plane. Like this: Plane[] terminalOne = new Plane[] { new Plane(1, "Madrid", "Ryanair", "Airbus A300",...
Jon Skeet
people
quotationmark

terminalOne is an array, not an individual plane. You could use: for (Plane plane : terminalOne) { plane.displayPlane(); } ... but I would personally consider overriding toString() in Plane instead: @Override public String... more 11/2/2013 8:42:26 AM

people

Use of unassigned local variable in a Try Catch

It seems that this particular error has been solved quite a few times but my code snippet has something different in that it will never cause an "unassigned" error. This code is...
Jon Skeet
people
quotationmark

my code snippet has something different in that it will never cause an "unassigned" error Well it clearly does cause that error, which is why you asked the question, no? Even though you know that any time the exception is thrown,... more 11/2/2013 8:30:49 AM

people

Overflow error when doing arithmetic operations with constants

I tried the following code : int x, y; x = y = int.MaxValue; int result = x + y; This code work fine and result will contain -2 (I know why). But when doing this : const int...
Jon Skeet
people
quotationmark

Because both x and y are compile-time constants, so is x + y. The compiler knows that the result will overflow, so it complains about it. You can fix this by using an unchecked expression: int result = unchecked(x + y); From section... more 11/1/2013 8:42:13 PM

people

C# How does dll references work?

I'm developing a client application which connects with a web service, in order to do that I'm using the WebClient class and the JavaScriptSerializer class for parse the requested...
Jon Skeet
people
quotationmark

It should be fine so long as the machine you're copying it to has the full version of .NET - not just a client profile. You shouldn't need to copy anything - it should just be fine to pull it from the GAC. Just make sure you've got the... more 11/1/2013 8:39:12 PM

people

How to define a private member behavior, as a method or property? C#

I have a object which is used in another context and I want to validate if a private member is null or empty (which is exposed by its property). It would be better to declare...
Jon Skeet
people
quotationmark

Well that certainly feels more like a property to me than a method - and it's certainly in-keeping with things like Nullable<T>.HasValue. Other differences to consider: You can generally bind against properties but not methods;... more 11/1/2013 8:27:15 PM

people

C# add XMLNode to XMLNodeList

This one I thought would be simple, but i really just have 1 node I need to add to a node List. So I have XmlNode xmlNode. This node has all the information I need in it. I...
Jon Skeet
people
quotationmark

How about: XmlNodeList xmlNodeList = xmlNode.SelectNodes("."); more 11/1/2013 5:07:26 PM

people

How do i serialize static class?

I need to serialize static class with BinaryFormatter, here is the code: void Serialize() { IFormatter formatter = new BinaryFormatter(); using...
Jon Skeet
people
quotationmark

You normally serialize instances. You can't have an instance of a static class, so it makes no sense to serialize it. If you need to serialize the static state of a static class, then you should probably make it non-static to start with.... more 11/1/2013 3:55:16 PM

people

Methods overloading with value and reference parameter types

I have the following code : class Calculator { public int Sum(int x, int y) { return x + y; } public int Sum(out int x, out int...
Jon Skeet
people
quotationmark

To quote slightly differently to the other answers, this is from section 3.6 of the C# 5 specification, which I find rather clearer and more precise than the "reference guide": Although out and ref parameter modifiers are considered... more 11/1/2013 3:34:41 PM

people

i am making mp3 player using c# but i am sufferring from this error "Cannot implicitly convert type 'string' to 'string[]"

i am making mp3 player using c# but i am sufferring from this error "Cannot implicitly convert type 'string' to 'string[]". namespace WindowsFormsApplication3 { public...
Jon Skeet
people
quotationmark

It looks like you're trying to handle the user opening multiple files. In that case, use FileDialog.FileNames instead of FileName. Ditto SafeFileNames. (I would also strongly recommend renaming the variables so their names are meaningful... more 11/1/2013 2:43:21 PM

people

Class initialization Property vs Field

I was wondering what's the best practice for class initialization. One can write: private myClass mc = new myClass(); Or: private myClass mc { get; set; } public Foo() { ...
Jon Skeet
people
quotationmark

If it's private, there's no significant benefit in making it a property. I'd just keep it as a field. I use properties as a way of communicating an API with other classes. more 11/1/2013 11:13:15 AM

people