In TypeScript I can declare an array such as:
const arr: { id: number; value: string; }[] = [];
Is there a shorthand way I could do something similar in C#?
var list = new List<{ int id; string value; }>();
I find myself mutating and mapping lists a lot and it gets cumbersome to explicitly declare classes and interfaces for each different operation.
Uhhm, you could use a List<()>
, so called named Tuples:
var list = new List<(int id, string value)>();
And use it in the same way as if you're working with a list of objects:
var obj = list.First();
Console.WriteLine($"{obj.id}-{obj.value}");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With