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}");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.