new []和new string []有什么区别? [重复]

时间:2022-01-16 13:05:14

Possible Duplicate:
What is this new[] a shorthand for?

可能重复:这个新的[]是什么简写?

Is there any difference between

两者之间有什么区别吗?

var strings = new string[] { "hello", "world" };

and

var strings2 = new[] { "hello", "world" };

6 个解决方案

#1


29  

In this case, no difference, as new[] will infer the provided values type as string.

在这种情况下,没有区别,因为new []会将提供的值类型推断为字符串。

See Implicitly typed arrays.

请参见隐式类型化数组。

#2


7  

No difference.

没有不同。

The second one is a syntactic-sugar called "Implicitly typed arrays", and both the expressions return an array of strings.

第二个是一个名为“Implicitly typed arrays”的语法糖,两个表达式都返回一个字符串数组。

When you don't specify the type of the array, it is inferred from the types of the elements used to initialize the array.
To allow the inference, the expression must satisfy the following condition:

如果未指定数组的类型,则可以从用于初始化数组的元素类型推断出数组的类型。要允许推理,表达式必须满足以下条件:

Considering an implicitly typed array expression like this:

考虑一个隐式类型的数组表达式,如下所示:

   var arr = new []{ obj1, ... , objn }

and the set of all the types of the elements in the initialization being:

以及初始化中所有元素类型的集合:

   S = {T1, ..., Tn}

to allow the inference (i.e. no compiler error) it must be possible for all the types { T1, ... , Tn } to be implicitly cast to one of the types in the set S.

为了允许推理(即没有编译器错误),必须可以将所有类型{T1,...,Tn}隐式地转换为集合S中的一个类型。

So, for example, given the following classes:

因此,例如,给定以下类:

class Base { }
class Derived1 : Base { }
class Derived2 : Base { }
class Derived3
{ 
    public static implicit operator Base(Derived3 m)
    { return null; }
}

This code compiles:

此代码编译:

var arr = new []{ new Derived1(), new Derived2(), new Derived3(), new Base()};

while the following does not:

而以下不是:

var arr = new []{ new Derived1(), new Derived2(), new Derived3() };

since in the first case all the 3 types can be implicitly cast to type Base, and Base type is inside the set S = { Derived1, Derived2, Derived3, Base }, while in the second case all the types cannot be cast to one type in the set S = { Derived1, Derived2, Derived3 }

因为在第一种情况下,所有3种类型都可以隐式转换为类型Base,而Base类型在集合S = {Derived1,Derived2,Derived3,Base}内,而在第二种情况下,所有类型都不能转换为一种类型在集合中S = {Derived1,Derived2,Derived3}


This feature has been introduced with C# 3.0 along with anonymous types and it makes instantiation of arrays of the latter easier.

此功能已随C#3.0与匿名类型一起引入,它使后者的数组实例化更容易。

For instance, this would be really hard to obtain without implicitly typed arrays:

例如,如果没有隐式类型的数组,这将很难获得:

var arrayOfAnonymous = new[] { new { A = 3, B = 4 }, new { A = 2, B = 5 } };

#3


6  

In this case, there is no difference. Because of hello and world are string;

在这种情况下,没有区别。因为你好,世界是字符串;

var strings2 = new[] { "hello", "world" };

creates a string[] array which is the same with first one.

创建一个string []数组,与第一个数组相同。

new []和new string []有什么区别? [重复]

Second one is just called Implicitly Typed Arrays

第二个被称为隐式类型数组

If we go one step further, they have the same IL code.

如果我们更进一步,他们有相同的IL代码。

#4


2  

None, the compile interprets it as new string[] { "hello", "world" };

无,编译将其解释为新字符串[] {“hello”,“world”};

It's just like using var, the compiler handles what you meant.

就像使用var一样,编译器会处理你的意思。

#5


1  

new[] creates an implicitly typed array in which the type is infered from the elements. while the other approach creates an array of string.

new []创建一个隐式类型数组,其中的类型来自元素。而另一种方法创建了一个字符串数组。

#6


0  

There is no difference. In the 2nd case, the C# compiler is smart enough to infer the type of the array, since it sees that the values that are used to initialize the array, are of type string.

没有区别。在第二种情况下,C#编译器足够聪明地推断出数组的类型,因为它看到用于初始化数组的值是string类型。

#1


29  

In this case, no difference, as new[] will infer the provided values type as string.

在这种情况下,没有区别,因为new []会将提供的值类型推断为字符串。

See Implicitly typed arrays.

请参见隐式类型化数组。

#2


7  

No difference.

没有不同。

The second one is a syntactic-sugar called "Implicitly typed arrays", and both the expressions return an array of strings.

第二个是一个名为“Implicitly typed arrays”的语法糖,两个表达式都返回一个字符串数组。

When you don't specify the type of the array, it is inferred from the types of the elements used to initialize the array.
To allow the inference, the expression must satisfy the following condition:

如果未指定数组的类型,则可以从用于初始化数组的元素类型推断出数组的类型。要允许推理,表达式必须满足以下条件:

Considering an implicitly typed array expression like this:

考虑一个隐式类型的数组表达式,如下所示:

   var arr = new []{ obj1, ... , objn }

and the set of all the types of the elements in the initialization being:

以及初始化中所有元素类型的集合:

   S = {T1, ..., Tn}

to allow the inference (i.e. no compiler error) it must be possible for all the types { T1, ... , Tn } to be implicitly cast to one of the types in the set S.

为了允许推理(即没有编译器错误),必须可以将所有类型{T1,...,Tn}隐式地转换为集合S中的一个类型。

So, for example, given the following classes:

因此,例如,给定以下类:

class Base { }
class Derived1 : Base { }
class Derived2 : Base { }
class Derived3
{ 
    public static implicit operator Base(Derived3 m)
    { return null; }
}

This code compiles:

此代码编译:

var arr = new []{ new Derived1(), new Derived2(), new Derived3(), new Base()};

while the following does not:

而以下不是:

var arr = new []{ new Derived1(), new Derived2(), new Derived3() };

since in the first case all the 3 types can be implicitly cast to type Base, and Base type is inside the set S = { Derived1, Derived2, Derived3, Base }, while in the second case all the types cannot be cast to one type in the set S = { Derived1, Derived2, Derived3 }

因为在第一种情况下,所有3种类型都可以隐式转换为类型Base,而Base类型在集合S = {Derived1,Derived2,Derived3,Base}内,而在第二种情况下,所有类型都不能转换为一种类型在集合中S = {Derived1,Derived2,Derived3}


This feature has been introduced with C# 3.0 along with anonymous types and it makes instantiation of arrays of the latter easier.

此功能已随C#3.0与匿名类型一起引入,它使后者的数组实例化更容易。

For instance, this would be really hard to obtain without implicitly typed arrays:

例如,如果没有隐式类型的数组,这将很难获得:

var arrayOfAnonymous = new[] { new { A = 3, B = 4 }, new { A = 2, B = 5 } };

#3


6  

In this case, there is no difference. Because of hello and world are string;

在这种情况下,没有区别。因为你好,世界是字符串;

var strings2 = new[] { "hello", "world" };

creates a string[] array which is the same with first one.

创建一个string []数组,与第一个数组相同。

new []和new string []有什么区别? [重复]

Second one is just called Implicitly Typed Arrays

第二个被称为隐式类型数组

If we go one step further, they have the same IL code.

如果我们更进一步,他们有相同的IL代码。

#4


2  

None, the compile interprets it as new string[] { "hello", "world" };

无,编译将其解释为新字符串[] {“hello”,“world”};

It's just like using var, the compiler handles what you meant.

就像使用var一样,编译器会处理你的意思。

#5


1  

new[] creates an implicitly typed array in which the type is infered from the elements. while the other approach creates an array of string.

new []创建一个隐式类型数组,其中的类型来自元素。而另一种方法创建了一个字符串数组。

#6


0  

There is no difference. In the 2nd case, the C# compiler is smart enough to infer the type of the array, since it sees that the values that are used to initialize the array, are of type string.

没有区别。在第二种情况下,C#编译器足够聪明地推断出数组的类型,因为它看到用于初始化数组的值是string类型。