Does T[] implement IList<T>? [duplicate] - c#

This question already has answers here:
How do arrays in C# partially implement IList<T>?
(6 answers)
Closed 8 years ago.
I have a class constructor that takes a IList<IElement> as an argument.
When creating a new instance of the class I'm able to pass a IElement[] instead of the IList<IElement> how is that posible?

An array with element type T derives from IList<T>.
This is not visible in the meta-data in mscorlib.dll, but the inheritance relationship is created at runtime in the CLR. C# and the CLR are aware of the array type and treat it specially.

Related

What is TElement in C# documentation? [duplicate]

This question already has answers here:
What are generics in C#? [closed]
(3 answers)
Understanding C# generics much better
(5 answers)
Closed 9 months ago.
In C# Documentations, there was an example under constructed types where they use class Queue , what is TElement and what does it mean /represent?
It's a placeholder for whatever type you wish to specify.
It could be Queue<string> or Queue<int> or Queue<List<int> or Queue<SomeComplicatedClass> - whatever you like.
It's called a "generic type parameter" (the bit between the < and the >).
The docs page has the following example:
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
You can read more about generic type parameters here.

Why do Arrays implement generic collections at run time? [duplicate]

This question already has answers here:
Why isn't Array a generic type?
(7 answers)
How do arrays in C# partially implement IList<T>?
(6 answers)
Relation between IEnumerable and arrays
(3 answers)
Closed 3 years ago.
In the documentation it says:
Single-dimensional arrays implement the
System.Collections.Generic.IList<T>,
System.Collections.Generic.ICollection<T>,
System.Collections.Generic.IEnumerable<T>,
System.Collections.Generic.IReadOnlyList<T> and
System.Collections.Generic.IReadOnlyCollection<T> generic interfaces.
The implementations are provided to arrays at run time.
Why are the provided at run-time, and not in the Array class definition?

Casting Abstract Type Object to Derived Type [duplicate]

This question already has answers here:
Is it possible to get c# to use method overload of most specific type rather than base type?
(4 answers)
up-casting in C# and call a specific method based on the derived type
(8 answers)
Closed 3 years ago.
I have a third party API that passes a parameter that is a base type. I handle each derived type separately as they have specialized functionality for each that I need to make use of.
if(parameter is DerivedTypeX) HandleParamerter(parameter as DerivedTypeX);
else if (parameter is DerivedTypeY) HandleParameter(parameter as DerivedTypeY);
...
For some reason, I can't let it go that there must be a more elegant way to handle this parameter than a massive else if block.

Make list immutable [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Immutable List in C#
Is it possible to make a list immutable
You can use ReadOnlyCollection<T> instead.
List<T>.AsReadOnly() returns a readonly wrapper, so that you can't add/remove/replace elements.
To be truly immutable, the type T must be an immutable Type.

What is Polymorphism? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Try to describe polymorphism as easy as you can
What is polymorphism?
Please read MSDN which covers it in reference to c#,
Basically a derived class inherits from another class it gets all its methods,events and properties, and every type is polymorphic in .NET since they all have Object as their base class.

Categories