There is a code where that reads something from the XML and loads it into dictionary.
Pls find the code below. I need to understand what new [] {header}
does.
what is new []
doing here.Thanks in advance.
var headers = _doc.GetElementsByTagName("Header");
var header = headers[0];
_dict.Add("Header_" + headerId, new [] {header});
It's an implicitly typed array creation expression - it lets the compiler infer the array type, a bit like using var
but for arrays. So in your case, it's equivalent to:
_dict.Add("Header_" + headerId, new XmlNode[] { header });
It was added in C# 3 along with var
to support anonymous types. For example, you can write:
var people = new[]
{
new { Name = "Jon", Home = "Reading" },
new { Name = "Gareth", Home = "Cambridge" }
};
You couldn't write that as explicitly typed array creation expression, because the type of the array elements doesn't have a name.
See more on this question at Stackoverflow