C#是否具有可与Python的列表解析媲美的功能? [英] Does C# have anything comparable to Python's list comprehensions?

查看:73
本文介绍了C#是否具有可与Python的列表解析媲美的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C#生成一个列表.我缺少python的列表理解.是否有C#方式像Python中的列表推导或生成器表达式那样快速创建集合?

I want to generate a list in C#. I am missing python's list comprehensions. Is there a C# way to create collections on the fly like list comprehensions or generator expressions do in python?

推荐答案

如果您使用的是C#3.0(VS2008),则LINQ to Objects可以做非常相似的事情:

If you are using C# 3.0 (VS2008) then LINQ to Objects can do very similar things:

List<Foo> fooList = new List<Foo>();
IEnumerable<Foo> extract = from foo in fooList where foo.Bar > 10 select Foo.Name.ToUpper();

这篇关于C#是否具有可与Python的列表解析媲美的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆