- Home /
How to use generic lists in C#?
I am unable to understand how to work with generic lists, I want to add class objects as items in the list and access items and alter object data when needed. The data in the list has to be permanently saved. Plz help Regards Nk
Answer by ShadyProductions · Jun 30, 2017 at 07:15 AM
public void AlterData<T>(List<T> data) //u can even add where T : classname
{
//DoAction
}
Do note that you cannot add different classes to one list unless they derive from some kind of base class.
you can't just do List.Add(1); and List.Add("1"); they are not the same even if the list is generic.
Actually I've created the list in the same class whose object I wanted to add to list and passed the constructor of the class as parameter to Add function, but the problem is I want to add around 20 objects and want to alter data when needed, this is what I'm unable to do
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Listing players by score 1 Answer
Getting NullReferenceException when creating class instance using List 1 Answer
Using the object.Equals() method for a custom class 1 Answer