简体   繁体   English

为什么我不能在没有初始化器的情况下声明隐式类型的数组?

[英]Why can't I declare an implicitly typed array without an initializer?

Why do I have to write int after new when I am declaring the array num without an initializer? 当我在没有初始化程序的情况下声明数组num时,为什么我必须在new之后编写int Eg 例如

int[] num = new int[5];

For another array, num1, I just declared it first and then gave it values, and I didn't type int after new like I did for array num . 对于另一个数组num1,我先声明它然后给它赋值,并且我没有像new的数组num那样输入int

int[] num1 = new [] {1, 2, 3, 4};

Why does this shortcut not work for my first example? 为什么这个快捷方式对我的第一个例子不起作用?

The type of the array can be inferred (under certain circumstances, which your example fullfills) if you provide the values for the array. 如果提供数组的值,则可以推断出数组的类型(在某些情况下,您的示例已满足)。 Since the values are all integers, the compiler is able to infer that you want an array of integers. 由于值都是整数,因此编译器能够推断出您需要一个整数数组。 If you don't provide any values (or if the values aren't all implicitly convertible to the type of one of the values), it has no way of inferring what the type of the array should be, and so you need to specify it explicitly. 如果您没有提供任何值(或者如果值不能全部隐式转换为其中一个值的类型),则无法推断出数组的类型应该是什么,因此您需要指定明确地说。

The "full" syntax for declaring an array with values is new int[] {1, 2, 3, 4}; 使用值声明数组的“完整”语法是new int[] {1, 2, 3, 4}; .

Your new [] {1, 2, 3, 4}; 你的new [] {1, 2, 3, 4}; line is a shorthand syntax. line是一种简写语法。 It's an "implicitly typed" array. 它是一个“隐式类型”数组。 Because the values are all int , the array is automatically declared an int[] . 因为值都是int ,所以数组自动声明为int[]

The reason you can't also write int[] x = new [5]; 你不能写int[] x = new [5]; is simply that this feature has not been implemented by the C# designers. 很简单,这个功能还没有被C#设计者实现。

Because you seem to expect that the way the compiler infers the type of the array is somehow based on the type to the left of the assignment operator: 因为您似乎期望编译器推断数组类型的方式基于赋值运算符左侧的类型:

int[] num = new[5];

Your thought process is: 你的思考过程是:

  1. num is an array of int s. num是一个int数组。
  2. new[5] is an array of something. new[5]是一系列的东西。
  3. something has to be int because that is num 's type. 有些事情是int ,因为这是num的类型。

That is not the way the compiler works. 这不是编译器的工作方式。 The compiler must figure out univocally the type to the right of the assignment expression and once it does, it will check if the assignment to whatever is on the left is valid. 编译器必须明确地找出赋值表达式右边的类型,一旦它完成,它将检查对左边的任何内容的赋值是否有效。

In the syntaxes allowed in C#, the compiler always has enough information to figure out the type on the right side of the assignment (if it has any). 在C#中允许的语法中,编译器总是有足够的信息来确定赋值右侧的类型(如果有的话)。 Your proposed syntax wouldn't provide that information. 您提出的语法不会提供该信息。

In your first scenario, you're declaring an empty array, so you need to determine what type it is. 在您的第一个场景中,您声明了一个空数组,因此您需要确定它是什么类型。

int[] num = new int[5]; // New empty array of integers with 5 slots.

However, in your second example, the type of the array is implied based on the values that you assign to the slots in the array. 但是,在第二个示例中,根据您为数组中的插槽分配的值隐含了数组的类型。

int[] num1 = new [] {1, 2, 3, 4}; // New array of integers with 4 slots, already assigned.

The above works because every element in the array is of the same type. 上面的工作原理是因为数组中的每个元素都是相同的类型。

If you were to do the following, a compilation error would occur because the type can't be inferred as the elements are different types. 如果您要执行以下操作,则会发生编译错误,因为无法推断类型,因为元素是不同的类型。

var array = new [] {1, "something"}; // This won't compile.

But, if you declare one of the elements as a common base type, then it will store all of the elements as that base type. 但是,如果将其中一个元素声明为公共基类型,则它将所有元素存储为该基类型。

var array = new [] {(object)1, "something", DateTime.Now}; // This is an array of objects.

why I have to write int after new when I am declaring array num with default values 当我用默认值声明数组num时,为什么我必须在new之后编写int

You don't. 你没有。 There are a number of shorthands possible because often the compiler can infer the type of the array you are creating, like in your case. 有许多可能的缩写,因为编译器通常可以推断出您正在创建的数组的类型 ,就像您的情况一样。

So this is a perfectly fine declaration: 所以这是一个非常好的声明:

var num1 = new[] { 1, 2, 3, 4 };

See, there is no int here, but if you hover over var it will tell you the array is an int[] . 看,这里没有int ,但如果你将鼠标悬停在var它会告诉你数组是一个int[]

It is getting harder when you are mixing types: 混合类型时越来越难:

var num1 = new [] { "1", 1 };

Then the compiler doesn't know and forces you to specify a type. 然后编译器不知道并强制您指定类型。

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

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