简体   繁体   English

这是如何将 python 生成器转换为 C#

[英]Is this how to translate python generators to C#

So I need this python code or alike it to translate to C# in a generator format所以我需要这个 python 代码或类似的代码以生成器格式转换为 C#

x = [5, 9, 0, 7, 2]
y = [i*i for i in x if i%2==0]
print(y) #outputs the full list

Will the resulting code will be this, assuming the Main block is written:假设 Main 块是这样写的,结果代码会是这样吗:

int[] x = new int[] {5, 9, 0, 7, 2};
var y = (from i in x where i%2 == 0 select i*i).ToArray();
Console.Writeline(y); //outputs Array object

It uses LINQ library它使用 LINQ 库

Try this:尝试这个:

var x = new [] { 5, 9, 0, 7, 2 };
var y = x.Where(n => n % 2 == 0).Select(m => m*=m);
Console.WriteLine($"[{string.Join(",",y)}]");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM