Search code examples
c#new-operator

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


I found this code

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

and can guess what it does, but cannot find an explanation why the type definition byte[] can be omitted. I looked at msdn c# new explanation, but that is too simple there.


Solution

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

    Taken from https://msdn.microsoft.com/en-us/library/bb384090.aspx