Raskolnikov Raskolnikov - 3 months ago 36
TypeScript Question

TypeScript "map" function in C#?

I have code in TypeScript and I need equivalent code in C#.


private sessionCommands: SessionCommand[];
// . . .
// Create array in constructor.
this.sessionCommands = new Array();
// . . .
// Push few objects to array in some method

And then get data. This is important part, how to do this in C# ?

var data = this.sessionCommands.map(x => x.identifier + " " + x.getParameter() + ";").join("\n");


The equivalent in the .NET world is the Select function:

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);

It works on all kinds of enumerable types (including arrays). However it's an extension method, and you have to import System.Linq to be able to use it.

Full example for your code:

var data = String.Join("\n", this.sessionCommands.Select(x => x.identifier + " " + x.getParameter() + ";"));