This question already has answers here:
Convert List<DerivedClass> to List<BaseClass>
(13 answers)
Closed 7 years ago.
Why I can do the following conversion:
List<String> strings = new List<String>();
IEnumerable<Object> iStrings = strings as IEnumerable<Object>;
List<Object> oStrings = iStrings.ToList();
But I can't do it straight:
List<String> strings = new List<String>();
List<Object> oStrings = strings as List<Object>;
IEnumerable<T> is covariant, meaning it can be applied to derived types of T. If you're at the zoo looking at an exhibit with several lions, it's perfectly reasonable to also say you're looking at several animals.
List<T> is not covariant, meaning it can not be applied to derived types. It
's not appropriate to call a lion exhibit a collection of animal when you consider adding items. You can't add a gazelle to a lion exhibit or bad things will happen.
When you call ToList<object> on an IEnumerable<object>, you create a new collection with the references typed to object. You can safely add new objects to that list. You can't just "cast" a List<string> to a List<object> because that would imply that you could add other objects besides strings, which is not the case because it is a list of strings.
Related
This question already has answers here:
Linq query and casting in c#
(2 answers)
Closed 10 months ago.
I have a Class, Food which has two derived classes: Meat and Vegetables.
If I have a List of Foods, I can get a (sub)list of Foods that are Meat using
List<Food> allTheFood = GetListOfFood();
List<Food> justTheMeats = allTheFood.Where(x => x is Meat);
But this gives me a list of type Food (where they all happen to be Meat), rather than a list of Meat.
Obviously, I could create a new empty List and then do a foreach and cast every individual item from justTheMeats from Food to Meat and add them into the new List, but that seems a convoluted approach.
Is there a nicer/easier/cleaner way to do this?
Thanks
Yes, use linq OfType
var justTheMeats = allTheFood.OfType<Meat>().ToList();
This question already has answers here:
Is it a good/acceptable practice to declare variable as interface type?
(5 answers)
Using Interface variables
(12 answers)
Closed 4 years ago.
I see this being done so often with ICollection and IEnumberable, seeing new objects being created from the Interface instead of the class itself. To make this simple, let's use IList vs List because I understand those two a lot more.
What is the difference between:
IList<string> People = new List<string>();
vs
List<string> People = new List<string>();
?
Similarly to IEnumerable and ICollections, can't you just use
Collections<string> People = new Collections<string>();
as well instead of "ICollections"? It's so confusing and why is this done?
This question already has answers here:
What's the implementation of List?
(2 answers)
Closed 6 years ago.
Is it an ArrayList? Or a LinkedList? If I don't specify and run:
List<int> x = new List<int>(10);
What is the type of list created?
The answer is in plain english in the documentation:
The List class is the generic equivalent of the ArrayList class. It implements the IList generic interface by using an array whose size is dynamically increased as required.
List - is a generic collection class.
It`s not an ArrayList or LinkedList.
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T> { ... }
But you can create ArrayList from List
var arrayList = new ArrayList(list);
This question already has answers here:
IEnumerable vs List - What to Use? How do they work?
(11 answers)
IList vs IEnumerable for Collections on Entities
(2 answers)
Closed 9 years ago.
I am confused. Can anybody help me to understand Difference between IEnumeration<T> instead and List<T>?
You mean IEnumerable<T>. It's the base interface of all collection types like arrays or generic List<T>.
You can for example create a List<int>:
List<int> ints = new List<int>(){ 1,2,3 };
But since it implements IEnumerable<T> you could also declare it in this way:
IEnumerable<int> ints = new List<int>(){ 1,2,3 };
This has the advantage that you cannot modify ints since Remove comes from ICollection<T>.
This question already has answers here:
How does one add a LinkedList<T> to a LinkedList<T> in C#?
(3 answers)
Closed 9 years ago.
Let's say I have the following:
LinkedList<int> list1 = new LinkedList<int>();
LinkedList<int> list2 = new LinkedList<int>();
list1.AddLast(1);
list1.AddLast(2);
list2.AddLast(1);
list2.AddLast(2);
As far as I know you cannot do the following;
list1.AddLast(list2.First);
and except the lists to be connected together.
What is the proper way to merge two LinkedLists in C#? I know there is a Union() method, but it seems like such strong point of LinkedList in C++ is that you can easily combine and break lists apart if need be.
The LinkedList class does not support chaining, splitting, cycles,
or other features that can leave the list in an inconsistent state.
It's equally simple if you use a List instead of a LinkedList. Here are a couple of ways to do the whole list.
LINQ;
var combinedList = list1.Concat(list2).ToList();
Other way I found on msdn;
List<int> combinedList = new List<int>();
combinedList.AddRange(list1);
combinedList.AddRange(list2);