gpt4 book ai didi

c# - C# 中的 TypeScript "map"函数?

转载 作者:太空宇宙 更新时间:2023-11-03 21:06:45 24 4
gpt4 key购买 nike

我有 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() + ";"));

关于c# - C# 中的 TypeScript "map"函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40759778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com