icollection

ICollection / ICollection<T> ambiguity problem

拈花ヽ惹草 提交于 2019-12-03 16:12:55
Just want to make simple extension for syntactic sygar : public static bool IsNotEmpty(this ICollection obj) { return ((obj != null) && (obj.Count > 0)); } public static bool IsNotEmpty<T>(this ICollection<T> obj) { return ((obj != null) && (obj.Count > 0)); } It works perfectly when I work with some collections, but when working with others I get The call is ambiguous between the following methods or properties: 'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.IList)' and 'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.Generic.ICollection)' Is there any canonical solution to this

ICollection - Get single value

懵懂的女人 提交于 2019-12-03 08:06:10
问题 What is the best way to get a value from a ICollection? We know the Collection is empty apart from that. 回答1: Linq, baby, yeah... 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(); 回答2: 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

Why do Queue(T) and Stack(T) not implement ICollection(T)?

蹲街弑〆低调 提交于 2019-12-03 05:08:40
问题 Before I even ask, let me get the obvious answer out of the way: The ICollection<T> interface includes a Remove method to remove an arbitrary element, which Queue<T> and Stack<T> can't really support (since they can only remove "end" elements). OK, I realize that. Actually, my question is not specifically about the Queue<T> or Stack<T> collection types; rather, it's about the design decision of not implementing ICollection<T> for any generic type that is essentially a collection of T values.

ICollection - Get single value

我是研究僧i 提交于 2019-12-02 21:37:12
What is the best way to get a value from a ICollection? We know the Collection is empty apart from that. Will Linq, baby, yeah... 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(); 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

Why do Queue(T) and Stack(T) not implement ICollection(T)?

有些话、适合烂在心里 提交于 2019-12-02 18:22:20
Before I even ask, let me get the obvious answer out of the way: The ICollection<T> interface includes a Remove method to remove an arbitrary element, which Queue<T> and Stack<T> can't really support (since they can only remove "end" elements). OK, I realize that. Actually, my question is not specifically about the Queue<T> or Stack<T> collection types; rather, it's about the design decision of not implementing ICollection<T> for any generic type that is essentially a collection of T values. Here's what I find odd. Say I have a method that accepts an arbitrary collection of T , and for the

I can not access Count property of the array but through casting to ICollection !

こ雲淡風輕ζ 提交于 2019-12-02 11:19:30
问题 int[] arr = new int[5]; Console.WriteLine(arr.Count.ToString());//Compiler Error Console.WriteLine(((ICollection)arr).Count.ToString());//works print 5 Console.WriteLine(arr.Length.ToString());//print 5 Do you have an explanation for that? 回答1: Arrays have .Length, not .Count. But this is available (as an explicit interface implementation ) on ICollection etc. Essentially, the same as: interface IFoo { int Foo { get; } } class Bar : IFoo { public int Value { get { return 12; } } int IFoo.Foo

.net - dictionary.Keys.Add?

痞子三分冷 提交于 2019-12-02 10:59:35
问题 In .Net, IDictionary<K, V> defines .Keys and .Values properties, each of which is an ICollection<> rather than IEnumerable<> , which seems like it would be a more natural fit to me. Is there any reasonable use case to call .Add or .Remove on .Keys or .Values of an instance of an IDictionary<K, V> ? 回答1: No, probably no reasonable use case. There are very few (probably zero) legitimate reasons for this at all. The Dictionary<TKey, TValue> class returns a KeyCollection for its .Keys which in

I can not access Count property of the array but through casting to ICollection !

浪尽此生 提交于 2019-12-02 05:19:23
int[] arr = new int[5]; Console.WriteLine(arr.Count.ToString());//Compiler Error Console.WriteLine(((ICollection)arr).Count.ToString());//works print 5 Console.WriteLine(arr.Length.ToString());//print 5 Do you have an explanation for that? Arrays have .Length, not .Count. But this is available (as an explicit interface implementation ) on ICollection etc. Essentially, the same as: interface IFoo { int Foo { get; } } class Bar : IFoo { public int Value { get { return 12; } } int IFoo.Foo { get { return Value; } } // explicit interface implementation } Bar doesn't have public a Foo property -

Replacing an element in ICollection

限于喜欢 提交于 2019-12-01 21:26:33
Suppose I have an ICollection<SomeClass> . I have the following two variables: SomeClass old; SomeClass new; How can I achieve something like the following using an ICollection<SomeClass> ? // old is guaranteed to be inside collection collection.Replace(old, new); There is no black magic here: ICollection<T> is not ordered and only provides Add / Remove methods. Your only solution would be to check if the actual implementation is something more , such as IList<T> : public static void Swap<T>(this ICollection<T> collection, T oldValue, T newValue) { // In case the collection is ordered, we'll

Why can I apply an indexer to an ICollection in VB.Net, but not in C#

你离开我真会死。 提交于 2019-12-01 15:09:29
Was converting some code from VB.Net to C#, when I came across this, in some code using the Ionic Zip library: Dim zipEntry1 As ZipEntry = zipFile1.Entries(0) Simple enough: ZipEntry zipEntry1 = zipFile1.Entries[0]; I get this error on C#: Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.ICollection' Both are using the same version of the DLL, on both zipFile1.Entries is a generic ICollection . I have tested the below on VB.Net, and it builds successfullly: Option Strict On Option Explicit On Imports Ionic.Zip Module Module1 Sub Main() Dim zipFile1 = ZipFile