开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜