Skip to main content
 首页 » 编程设计

c#之C# 中的 TypeScript "map"函数

2024年02月05日32java哥

我有 TypeScript 代码,我需要 C# 中的等效代码。

声明:

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

然后获取数据。这是重要的部分,如何在 C# 中执行此操作?

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

请您参考如下方法:

.NET 世界中的等效项是 Select 函数:

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

它适用于各种可枚举类型(包括数组)。然而,它是一种扩展方法,您必须导入 System.Linq 才能使用它。

您的代码的完整示例:

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