new[] {a,b} 是什么意思和创造?

What does new[] {a,b} mean and create?

我找到了这段代码

Stream foo() 
{
  ...
  return new MemoryStream(new[] { a, b });
}

并且可以猜出它的作用,但无法找到为什么可以省略类型定义 byte[] 的解释。 我看了 msdn c# new 的解释,但是那里太简单了。

I can guess what it does

你说的对,和new byte[] { a, b }

一样

I cannot find an explanation why the type definition byte[] can be omitted

之所以可以省略,是因为ab都是byte类型。编译器足够聪明,可以判断出您打算创建一个 byte 数组,因此它不会要求您明确指定类型。

Microsoft 解释了此快捷方式 here。当可以从初始化上下文中推断出实际类型时,这个想法类似于在使用 var 的声明中省略类型。

类型定义可以省略,因为它可以从列表中对象的类型推断出来。

C# 编译器会在编译时对代码进行大量分析,以检查您编写的内容是否有效以及是否会按预期执行。您仍然会犯错误,但(希望)比在 C 或 C++ 等语言中少。

You can create an implicitly-typed array in which the type of the array instance is inferred from the elements specified in the array initializer. The rules for any implicitly-typed variable also apply to implicitly-typed arrays.

取自https://msdn.microsoft.com/en-us/library/bb384090.aspx

有了它,基本上您隐式地创建了一个对象数组!