ICollection - 获取单个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/363326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:36:37  来源:igfitidea点击:

ICollection - Get single value

c#.neticollection

提问by

What is the best way to get a value from a ICollection? We know the Collection is empty apart from that.

从 ICollection 获取值的最佳方法是什么?除此之外,我们知道 Collection 是空的。

回答by Chris

The simplest way to do this is:

最简单的方法是:

foreach(object o in collection) {
  return o;
}

But this isn't particularly efficient if it's actually a generic collection because IEnumerator implements IDisposable, so the compiler has to put in a try/finally, with a Dispose() call in the finally block.

但是如果它实际上是一个泛型集合,这并不是特别有效,因为 IEnumerator 实现了 IDisposable,所以编译器必须放入 try/finally,并在 finally 块中调用 Dispose()。

If it's a non-generic collection, or you know the generic collection implements nothing in its Dispose() method, then the following can be used:

如果它是非泛型集合,或者您知道泛型集合在其 Dispose() 方法中没有实现任何内容,则可以使用以下内容:

IEnumerator en = collection.GetEnumerator();
en.MoveNext();
return en.Current;

If you know if may implement IList, you can do this:

如果你知道是否可以实现 IList,你可以这样做:

IList iList = collection as IList;
if (iList != null) {
  // Implements IList, so can use indexer
  return iList[0];
}
// Use the slower way
foreach (object o in collection) {
  return o;
}

Likewise, if it's likely it'll be of a certain type of your own definition that has some kind of indexed access, you can use the same technique.

同样,如果它可能属于您自己定义的某种类型,并且具有某种索引访问,您可以使用相同的技术。

回答by bruno conde

Without generics and because ICollectionimplements IEnumerableyou can do like in example 1. With generics you simple need to do like example 2:

没有泛型,因为ICollection实现,IEnumerable你可以像示例 1 那样做。 使用泛型,你只需要像示例 2 那样做:

List<string> l = new List<string>();
l.Add("astring");

ICollection col1 = (ICollection)l;
ICollection<string> col2 = (ICollection<string>)l;

//example 1
IEnumerator e1 = col1.GetEnumerator();
if (e1.MoveNext())
    Console.WriteLine(e1.Current);

//example 2
if (col2.Count != 0)
    Console.WriteLine(col2.Single());

回答by bruno conde

Linq, baby, yeah...

Linq,宝贝,是的...

   var foo = myICollection.OfType<YourType>().FirstOrDefault();
    // or use a query
    var bar = (from x in myICollection.OfType<YourType>() where x.SomeProperty == someValue select x)
       .FirstOrDefault();

回答by mehrdad seyrafi

.

.

collection.ToArray()[i]

This way is slow, but very simple to use

这种方式速度慢,但使用起来非常简单

回答by JAD

If you know your collection has only one item, should only ever have one item, you can use the Linq extension method Single().

如果你知道你的集合只有一项,应该只有一项,你可以使用 Linq 扩展方法Single()

This converts a ICollection<T>into a Tobject containing the single item of that collection. If the length of the collection is 0, or more than one, this will throw an InvalidOperationException.

这会将 aICollection<T>转换为T包含该集合的单个项目的对象。如果集合的长度为 0 或大于 1,则会抛出InvalidOperationException.