简体   繁体   English

C#中的值分配

[英]Value assignment in C#

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.

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. 注意,数组引用不被分配给s ,直到所有的元素都被分配。 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. 对象和集合初始化程序也是如此-变量仅在末尾分配。

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 看看这个http://msdn.microsoft.com/en-us/library/0a7fscd0%28VS.71%29.aspx

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. 虽然它仅在数组类型的变量的初始值设定项中允许,但它完全等效于new T[] { ... } ,其中T是从变量类型推导出的。

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. 就像var关键字一样,关键是要限制您键入的冗余信息的数量。

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. 有关更多详细信息,请参见MSDN (“初始化数组”部分)。

The {"all","in","all"} part is the initialization. {"all","in","all"}部分是初始化。 the new string[] part can be omitted because the curly braces and string are short hand notation. new string[]部分可以省略,因为花括号和string是简写形式。 Take a look at MSDN on Single Dimension Arrays. 看一看MSDN上的一维数组。

You don't need the new string[] part in C#3 or higher - this works fine 您不需要C#3或更高版本中的新string []部分-可以正常工作

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. 这只是一个例子,编译器更加智能,可以理解您的意思-后端IL相同。

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. 考虑一下糖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM