Question

I had just started learning C# and using Tutorials Point to do this. In the section on arrays, I keep seeing arrays declared as

string[] sarray = { "Hello", "From", "Tutorials", "Point" };

Or other times like this:

int [] marks = new int[5]  { 99,  98, 92, 97, 95 };

Coming from c++, the first method of declaration wasn't all that new to me, but the second one is the one I don't understand. The question is if the array is going to dynamically initialised, then why use the brace enclosed initializer list to specify the contents of the array?

Is there an advantage to initialising that second one the way it has been done?

Was it helpful?

Solution

No advantage at all. It's a coding-preference thing.

You can even leave out just the size portion such as:

int[] marks = new int[] {99, 98, 92, 97, 95};

Check out the MSDN for more info: http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx

OTHER TIPS

The first statement is implicitly the same as

string[] sarray = new string[5] { "Hello", ... };

There is no advantage to one style over the other, save personal preference. I would tend to write the second statement as

var marks = new [] { 99, 98, ... };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top