Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders posts can be found here. LINQ has added so many goodies that can be used to query IEnumerable<T> sequences that it can be easy to lose sight of some of the methods that are unique to each of the collection classes. This week, we will look at the range series of methods in the List<T> collection, which are useful for getting, adding, inserting, and deleting ranges to/from a List<T>.
AddRange() – Appends a sequence to endSometimes, you may have a list of items and need to append another sequence of items to the end of a List<T>. The AddRange() method can be used for this very purpose: AddRange(IEnumerable<T> sequence)
Adds the elements of the sequence of type T to the end of the List<T>.The elements of the sequence are added to the end of the existing list. If the list is empty, the elements from the sequence will be the only thing in the list. If the sequence itself is empty, the list will be unchanged. 1: var primes = new[] {2, 3, 5, 7, 11 };
2: var list = new List<int>();
3:
4: // list was empty, now contains 2, 3, 5, 7, 11
5: list.AddRange(primes);
6:
7: // list still contains 2, 3, 5, 7, 11 since seqeunce empty
8: list.AddRange(Enumerable.Empty<int>());
9:
10: // list now contains 2, 3, 5, 7, 11, 13, 17
11: list.AddRange(new[] { 13, 17 });This method can be very handy if you are looking to consolidate the results from several methods that return sequences into one List<T>, you could construct a List<T> and then use AddRange() to add each subsequent result sequence at the end of the list: 1: var allSymbols = new List<string>();
3: foreach (var securityType in availableTypes)
4: {
5: IEnumerable<string> symbolsForType = GetSymbols(securityType);
7: // AddRange() adds sequence to end of list
8: allSymbols.AddRange(symbolsForType);
9: }
10:
11: // allSymbols now contains all of the results
12: return allSymbols;While AddRange() does not cause any elements of the existing List<T> to need to shift, it may cause a re-allocation if the new size of the list would exceed it’s current capacity. Read more: James Michael Hare
QR:
AddRange() – Appends a sequence to endSometimes, you may have a list of items and need to append another sequence of items to the end of a List<T>. The AddRange() method can be used for this very purpose: AddRange(IEnumerable<T> sequence)
Adds the elements of the sequence of type T to the end of the List<T>.The elements of the sequence are added to the end of the existing list. If the list is empty, the elements from the sequence will be the only thing in the list. If the sequence itself is empty, the list will be unchanged. 1: var primes = new[] {2, 3, 5, 7, 11 };
2: var list = new List<int>();
3:
4: // list was empty, now contains 2, 3, 5, 7, 11
5: list.AddRange(primes);
6:
7: // list still contains 2, 3, 5, 7, 11 since seqeunce empty
8: list.AddRange(Enumerable.Empty<int>());
9:
10: // list now contains 2, 3, 5, 7, 11, 13, 17
11: list.AddRange(new[] { 13, 17 });This method can be very handy if you are looking to consolidate the results from several methods that return sequences into one List<T>, you could construct a List<T> and then use AddRange() to add each subsequent result sequence at the end of the list: 1: var allSymbols = new List<string>();
3: foreach (var securityType in availableTypes)
4: {
5: IEnumerable<string> symbolsForType = GetSymbols(securityType);
7: // AddRange() adds sequence to end of list
8: allSymbols.AddRange(symbolsForType);
9: }
10:
11: // allSymbols now contains all of the results
12: return allSymbols;While AddRange() does not cause any elements of the existing List<T> to need to shift, it may cause a re-allocation if the new size of the list would exceed it’s current capacity. Read more: James Michael Hare
QR:
0 comments:
Post a Comment