.net 枚举第一个和最后一个
.NET(或某种标准扩展方法)中有没有办法提出枚举问题?
例如,当前项是枚举中的第一项还是最后一项:
string s = "";
foreach (var person in PeopleListEnumerator) {
if (PeopleListEnumerator.IsFirstItem) s += "[";
s += person.ToString();
if (!PeopleListEnumerator.IsLastItem) s += ",";
else s += "]";
}
is there a way in .NET (or some sort of standard extension methods) to ask questions of an enumeration?
For example is the current item the first or last item in the enumeration:
string s = "";
foreach (var person in PeopleListEnumerator) {
if (PeopleListEnumerator.IsFirstItem) s += "[";
s += person.ToString();
if (!PeopleListEnumerator.IsLastItem) s += ",";
else s += "]";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用 LINQ,您可以执行以下操作:
Using LINQ, you can do:
我发现自己定义并使用“bool first”局部变量,例如:
I find myself defining and using a "bool first" local variable, for example:
此代码与您真正想要完成的任务非常匹配:
This code closely matches what you really want to accomplish:
我给这只猫剥皮的方法(是的,有很多方法)是:
希望这有帮助......
The way I'd skin this cat (yes, there are that many ways) would be:
Hope this helps...
在 .NET 中,枚举没有顺序概念,除了按值排序。 如果您想要第一个和最后一个枚举,我建议明确定义这些第一个和最后一个值是什么。 以下枚举是一个示例:
然后您可以表达类似于以下内容的内容:
In .NET enumerations have no concept of order, except by value. If you want a first and last enumeration I would suggest explicitly defining what these first and last values are. The following enumeration is an example of this:
You can then express something similar to the following:
只是为了好玩,针对一般问题的解决方案不需要急于求值并且具有单个局部变量(枚举器除外):
测试:
Just for the sake of fun, a solution to the general problem that doesn't require eager evaluation and has a single local variable (except the enumerator):
Test:
如果您的集合是列表,您可以执行以下操作:
If your collection is a List, you can do:
从来没听说过。 不过,您可以尝试自己编写此类扩展方法。 或者在其他地方跟踪第一个/最后一个项目。
Eric Lippert 有一篇博客文章最近关于此类问题以及关于如何修改问题描述以更准确地反映您实际想要的内容的一些想法。
您最好使用 String.Join 和一些 LINQ在这种情况下:
Not that I know of. You can try writing such extension methods yourself, though. Or keep track of the first/last item elsewhere.
Eric Lippert had a blog post about that kind of problem recently along with some thoughts about how to modify your problem description to more accurately reflect what you actually want.
You're probably better off using a String.Join with some LINQ in this case:
IEnumerable 接口不定义也不期望它返回的项目按任何给定的顺序排列。 所以“第一”的概念并不总是适用。
然而,对于这个特定的模式,这就是我所做的
The IEnumerable interface does not define or expect the items that it returns to be in any given order. So the concept of "First" doesn't always apply.
However, for this particular pattern this is what I do
Jon Skeet 编写了智能枚举来提供此功能某种功能,它们是 MiscUtils 库的一部分。
这就是说,正如许多其他人指出的那样,您的具体示例最好通过 String.Join() 方法来解决。 编写通用的
string Join(this IEnumerable,string)
扩展并不难,并且可以在任何更多情况下使用,而不必求助于烦人的临时数组。Jon Skeet wrote Smart Enumerations to provide this sort of functionality and they are part of the MiscUtils library.
That said your specific example is best solved by the String.Join() approach as many others have pointed out. Writing a general
string Join(this IEnumerable<T>,string)
extension is not hard and is then usable in any more situations without having to resort to annoying temporary arrays.