+ 1

C#: Can List<> contain 3 or more parameters?

Like if I need to have id, name and count(n) how many stuff every of it have OR maybe C# have another list-like comands to serve my needs?

16th Aug 2019, 11:16 AM
Dead Lord
Dead Lord - avatar
5 ответов
+ 1
You should build a class with all of these 3 parameters and create a List of that object type. In my opinion it is a better solution than find a collection with three indexes.
16th Aug 2019, 12:40 PM
DonPietro Cavastano
DonPietro Cavastano - avatar
+ 1
DonPietro Cavastano but how can I edit or just search some info in that kind of list? Same example: I created a list of classes and I need to find one man in there
17th Aug 2019, 12:13 PM
Dead Lord
Dead Lord - avatar
+ 1
Linq - language integrated query allows to create Sql like queries. From System.Linq namespace. Let's say that your collection is a List<Person> and is called Persons. Where class person have the following properties, Name, Id, Count. You can use for example Where extension method that comes from the System.Linq which allows you to get elements from the list by certain conditions: var persons = Persons.Where(p => p.Name == "Tomasz"); // will return all objects from the list that attritbue Name is equal to Tomasz. For more information I would recommend you to read Msdn about the Linq.
17th Aug 2019, 1:30 PM
Tomasz
Tomasz - avatar
+ 1
Access to the member "name" through the link to the instance is not possible; specify it instead by specifying a type name Here what I get. How can I solve it? Tomasz
20th Aug 2019, 11:58 AM
Dead Lord
Dead Lord - avatar
0
Alternatively you can use Tuple<T1,T2,....,Tn>, in your example it would be Tuple<int, string, int>, however DonPietro solution would be much cleaner and more readable.
17th Aug 2019, 9:59 AM
Tomasz
Tomasz - avatar