开发者

How do I insert an array entry at initialisation time?

开发者 https://www.devze.com 2022-12-19 10:04 出处:网络
Given an array of strings, how do I create another array that has another entry inserted 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

精彩评论

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