The two solutions presented are bad, one creates a class class and allocates like crazy being inefficient in memory and processing and another makes the element like a dictionary that doesn’t have the same semantics of a list of lists, or even what you want, by what you’re saying, because what you want is a list of objects and not lists.
The appropriate solution for what you want (for what you have shown and not for what you have said) has a chance to be the creation of a struct
take care of it, something like that:
struct Item { public string AdminId, public string GroupId }
var lista = new List<Item>() { new Item { var1, var2 }, };
I used the name Item
Because I don’t know what this is about, it should be a better name. I did it in a very simplified way, probably it would be better to do something more complete than this for several reasons.
On the other hand, this may be something fleeting and I shouldn’t create a type to manage it, which I could only know if I had more details of the application. Then a tuple would be more suitable, but it would be the modern tuple of C# 7. Something like this:
var lista = new List<(string AdminId, string GroupId)>() { (var1, var2), };
That one var1
and var2
are local variables with the content, it doesn’t have to be that well, if the question had the code to create I would make a more detailed example. I took the opportunity to show how to initialize the code, and of course I would have to use several items. It may be more interesting to initialize later in a loop:
foreach (var item in fonteDeDados) lista.Add((item.AdminId, item.GroupId));
Or using the structure:
foreach (var item in fonteDeDados) lista.Add(new Item() { item.AdminId, item.GroupId });
I put in the Github for future reference.
Of course, I’m considering that the data source that will mount your list comes from an object that has the fields or properties with the names I used.
use a
List<dynamic>
would not solve?– Thiago Magalhães
But how would I bring string pairs into it? If I go through objects with attributes ONLY FOR THIS, it would not solve the problem.
– Leonardo Bonetti
Possible duplicate of What is Tuple and when to use?
– Matheus Miranda