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

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?

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.

Implicit/Explicit conversion between two lists/IEnumerable in c# [duplicate]

This question already has answers here:
Convert List<DerivedClass> to List<BaseClass>
(13 answers)
C# adding implict conversions to existing types
(2 answers)
Closed 2 years ago.
Can I make Implicit/Explicit conversion between two lists i.e.
List<Person> person = new List<Human>();

How to declare an Optional type in C#? [duplicate]

This question already has answers here:
Optional return in C#.Net
(13 answers)
Closed 8 years ago.
Java 8 has Optional<T> which is nice way to declare optional types as described here.
Is there an equivalent way to that in C# ?
As per this answer You could make a struct that mimics this this type of functionality.

Does T[] implement IList<T>? [duplicate]

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.

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.

Categories