Why create new object from interface? [duplicate] - c#

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?

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.

C#: String to Class [duplicate]

This question already has answers here:
Create a generic list using reflection
(3 answers)
Closed 8 years ago.
I am trying to initialize a Generic List object from Type name having as String like below:
List<(Type.GetType("CustomClass"))> AvroList = new List<(Type.GetType("CustomClass"))>();
What is the correct way to do it?
Question Update:
Also I need like below which is a statement with error
var AvroList = GetList<Type.GetType("CustomClass")>();
GetList has some logic which returns the list.
List<T> GetList<T>()
{
}
You need to use MakeGenericType method and then use Activator.CreateInstance to create an instance of your generic type:
var type = typeof(List<>).MakeGenericType(Type.GetType("CustomClass"));
var list = (IList)Activator.CreateInstance(type);
Since you don't now the type at compile time, casting IList is best thing you can do.You can also use dynamic but it is not safe either.

Difference between IEnumeration<T> instead and List<T>? [duplicate]

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>.

Create anonymous type with mutable fields? [duplicate]

This question already has answers here:
Non-read only alternative to anonymous types
(7 answers)
Closed 10 years ago.
I can create an anonymous class easily using the below. However I can't write to it. Is there a way I can write to some sort of unnamed class?
var test = new { a = 5, b = "sz" };
test.a++;
This is not possible with anonymous types because the properties of an anonymous class are read-only. As to why, you can read here:
Why are the properties of anonymous types in C# read-only?

var in C# - Why can't it be used as a member variable? [duplicate]

This question already has answers here:
Implicit typing; why just local variables?
(6 answers)
Closed 8 years ago.
Why is it not possible to have implicitly-typed variables at a class level within C# for when these variables are immediately assigned?
ie:
public class TheClass
{
private var aList = new List<string>();
}
Is it just something that hasn't been implemented or is there a conceptual/technical reason for why it hasn't been done?
Here's a blog post from Eric that explains the reasoning.

Categories