Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Inserting items to a collection,

MyList.Clear();
MyList.InsertRange(MyList.Count, Collection1);
MyList.InsertRange(MyList.Count, Collection2);
MyList.InsertRange(MyList.Count, Collection3);

In the above code, I am inserting 3 times to the same list in order to follow the sequence. Can this be replaced efficiently?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
262 views
Welcome To Ask or Share your Answers For Others

1 Answer

Not sure if this is more efficient:

MyList.AddRange(Collection1.Concat(Collection2).Concat(Collection3));

If the collections are large it will probably more efficient if you prepend this line:

MyList = new List<string>(Collection1.Count + Collection2.Count + Collection3.Count);

(presumed it's a List<string>)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...