Merge List <T> of the same type

I have 2 lists whose defs:

List<Type> filteredTypes = new List<Type>(); List<Type> interfaceTypes = new List<Type>(); 

When my lists are full, I would like to get one loop for both of them, and my idea is to combine them before the "loop", so I don't need to use LINQ (I don't like it) ..-_-) I checked online is a document, and I think I should do:

 filteredTypes.Concat(interfaceTypes); 

I debugged as deep as I could, and my lists match after the instructions ... What am I missing?

+4
source share
6 answers

The concatenated function returns new collection; it does not add to the existing one.

 var allTypes = filteredTypes.Concat(interfaceTypes); 
+7
source

See here: .NET List <T> Concat vs AddRange

calling .Concat() creates a new List<T> , so you need something like:

 var mergedList = filteredTypes.Concat(interfaceTypes); 
+4
source

Concat returns a new list without changing one of the original lists. If you want to put it in a new list, do the following:

 List<Type> newList = filteredTypes.Concat(interfaceTypes); 

If you want to put it in one of the old lists, use AddRange:

 filteredTypes.AddRange(interfaceTypes); 
+3
source

Why not use AddRange?

  filteredTypes.AddRange(interfaceTypes); 
0
source

IEnumerable<T>.Concat(IEnumerable<T>) returns IEnumerable<T>.

Try the following:

 var resultTypes = filteredTypes.Concat(interfaceTypes); 
0
source

You need to do:

 var temp = firstList.Concat(secondList); List<Type> finalList = temp.ToList<Type>(); 
0
source

Source: https://habr.com/ru/post/1384940/


All Articles