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.
Related
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.
This question already has answers here:
Creating a task wrapper around an existing object
(3 answers)
Closed 2 years ago.
Is there a way to convert a list from List<> to Task<List<>> ? I know Task<List<>> to List<> but I don't know to other direction.
Thanks,
Have you tried Task.FromResult(list)? Where list is the variable storing reference to your Collection.
I think you want Task.FromResult(* your List<> here *)
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why it is not posible to define generic indexers in .NET?
how to write a function to take any object with an index operator
I've never seen any usage like that. But I just wonder if it is possible to make an implementation like bleow. I know that it's not working. But I mean a similar usage if exist.
public T this<T>[T param]
{
get
{
....
}
}
No, generic properties, and indexers (a property), aren't possible.
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.