Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline/shorthand types in C#

Tags:

c#

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.

like image 320
Reed Avatar asked Jan 22 '20 17:01

Reed


1 Answers

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}");
like image 75
Fabjan Avatar answered Nov 20 '22 14:11

Fabjan