How do I insert an array entry at initialisation time?
Given an array of strings, how do I create another array that has another entry inserted at initialisation time?
Eg. I want to do something like this:
var newArrayOfStrings = new string[] { "inse开发者_高级运维rted entry", anotherArrayOfStrings }
(I know I can do this by getting a count and then copying; but I think it should be possible to do this at initialisation time.)
No, there's no special syntax for doing that. You'll need to do the copying in one way or another:
var newArrayOfStrings =
new[] { "inserted entry" }.Concat(anotherArrayOfStrings).ToArray();
Performance-wise, this can be slower than Array.Copy
but the syntax is cleaner.
精彩评论