views:

187

answers:

8

Without initialization how is it possible to assign values to arrays?

string[] s={"all","in","all"};

I mean why did not the compile show error?.Normally we need to 
initialize ,before assign values.
+1  A: 
string[] s = new string[] { "all","in","all"};

and its shorthand version

string[] s = {"all","in","all"};

are the same thing. See MSDN (Initializing Arrays section) for more detail.

ee
+1  A: 

The {"all","in","all"} part is the initialization. the new string[] part can be omitted because the curly braces and string are short hand notation. Take a look at MSDN on Single Dimension Arrays.

Bob
ah. Good to know. I have always done it explicitly.
ee
+3  A: 

It is possible to declare an array variable without initialization.

Check this out http://msdn.microsoft.com/en-us/library/0a7fscd0%28VS.71%29.aspx

madan
declaring an array without initialization would just be `string[] s` the example above provides values which the string is initialized with.
Bob
the example above provides values which the string *array* is initialized with
Bob
+2  A: 

I think you want to know why

 string[] s={"all","in","all"};

works when you would expect to be required to initialize the array first like this :

string[] s = new string[];

or

string[] s = new string[] {"all","in","all"};

The answer is just compiler magic. The compiler knows based on the initialization how big to make the array so it just does it behind the scenes for you. Much like the var keyword, the point is to limit the amount of redundant information you're required to type.

Jon Norton
Is this relaxation for arrays only? Pesron a={ Name="somename"} ??
+3  A: 

You aren't "assigning a value to array". You are initializing a variable of type "reference to array". The value with which you initialize it is a reference to an array which was created by the use of short array initializer syntax {...}. While it is only permissible in initializer of variables of array type, it is exactly equivalent to new T[] { ... }, where T is deduced from type of variable.

Pavel Minaev
+1  A: 

You don't need the new string[] part in C#3 or higher - this works fine

string[] s = { "all","in","all"};

It's just a case of the compiler being a bit smarter and working out what you mean - the back end IL will be the same.

Dan Diplo
+1  A: 

You can do so simply because it is allowed, doing so in two steps is not necessary so this is the shorthand. Consider it sugar.

Seth
+6  A: 

It's just syntactic sugar.

This:

string[] s = {"all","in","all"};

is compiled to the same code as:

string[] tmp = new string[3];
tmp[0] = "all";
tmp[1] = "in";
tmp[2] = "all";
string[] s = tmp;

Note that the array reference is not assigned to s until all the elements have been assigned. That isn't important in this particular case where we're declaring a new variable, but it would make a different in this situation:

string[] s = { "first", "second" };
s = new string[] { s[1], s[0] };

The same is true for object and collection initializers - the variable is only assigned at the end.

Jon Skeet
Is this relaxation for Array only?
ooh I found a bug in John Skeet's code! s is not defined, line 2. I think you meant tmp[0] = "all";
Neil N
Doh - thanks :)
Jon Skeet
@generix: What exactly do you mean by "relaxation"? It's just a form of syntax.
Jon Skeet
@Neil heheh...I confirm...it did just happen :D
ee
@Jon. I understood. Thanks.
@Jon Regardless of "bug", yet another interesting and informative answer.
ee
Interestingly enough, int[] x = {1, 2, 3} does NOT generate the same code as int[] tmp = new int[3]; tmp[0] = 1; tmp[1] = 2; tmp[2] = 3; int[] x = tmp. :-)
Eric Lippert
@Eric: Now you've got me intrigued now... will have to investigate :)
Jon Skeet
You'll be investigating our "private implementation details" then. (Integer arrays initialized with a block of constants are stored in a binary block named "PrivateImplemetationDetails" that can be passed to a special array initializer helper that efficiently blits the block into the array; that's much faster than copying in each element one at a time.)
Eric Lippert
I figured it would be something a bit like that. And you can't do that with strings because of garbage collection? Presumably you could do it with any immutable value type though...
Jon Skeet
Why would they want to do it with strings? There's `ldstr` for strings.
Pavel Minaev
@Pavel: Actually I withdraw my garbage collection comment - this is only appropriate for constants anyway. But `ldstr` is only for a single strings, not arrays of strings, right?
Jon Skeet