0

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.
7
  • Just like you showed... Or I don't get the question-part here. Commented Oct 21, 2009 at 18:33
  • I'm not sure I get the question. Commented Oct 21, 2009 at 18:36
  • Have we discovered the stackoverflow equivalent of 'how is babby formed?' Commented Oct 21, 2009 at 18:45
  • 1
    @Seth Smart answer.But Just i finised schooling.I am learning C#.If you know answer of your question explain me Mr Mega Genious. Commented Oct 21, 2009 at 18:49
  • 2
    You should read section 7.5.10 of the C# specification; it answers all your questions that you've posted here -- how array, object and collection initializers work in C# 3.0. Commented Oct 21, 2009 at 20:58

8 Answers 8

6

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.

Sign up to request clarification or add additional context in comments.

11 Comments

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";
@generix: What exactly do you mean by "relaxation"? It's just a form of syntax.
@Neil heheh...I confirm...it did just happen :D
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. :-)
|
3

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

2 Comments

declaring an array without initialization would just be string[] s the example above provides values which the string is initialized with.
the example above provides values which the string array is initialized with
3

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.

Comments

2

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.

1 Comment

Is this relaxation for arrays only? Pesron a={ Name="somename"} ??
1

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.

Comments

1
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.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.