Questions tagged [icollection]

The ICollection interface is the base interface for classes in the System.Collections namespace. It defines size, enumerators, and synchronization methods for all non-generic collections

The ICollection interface is the base interface for classes in the System.Collections namespace.

The ICollection interface extends IEnumerable; IDictionary and IList are more specialized interfaces that extend ICollection. An IDictionary implementation is a collection of key/value pairs, like the Hashtable class. An IList implementation is a collection of values and its members can be accessed by index, like the ArrayList class.

Some collections that limit access to their elements, such as the Queue class and the Stack class, directly implement the ICollection interface.

If neither the IDictionary interface nor the IList interface meet the requirements of the required collection, derive the new collection class from the ICollection interface instead for more flexibility.

279 questions
15
votes
7 answers

Must IList be finite?

Must .NET's IList be finite? Suppose I write a class FibonacciList implementing IList The property Item[n] returns the nth Fibonacci number. The property IsReadOnly returns true. The methods IndexOf and Contains we can implement easily…
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
14
votes
4 answers

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

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…
JMK
  • 27,273
  • 52
  • 163
  • 280
13
votes
3 answers

how to set an empty iCollection c#

I have three collections: private ICollection wrassets; private ICollection wrfunds; private ICollection wrstrats; If a foreach loop returns 0 objects, the collections don't get set and are…
Marc Howard
  • 395
  • 2
  • 6
  • 25
13
votes
2 answers

Using LINQ with classes implementing non-generic ICollection

I wanted to run a LINQ query against a MatchCollection object but found this wasn't possible as it doesn't implement ICollection, just ICollection. What is the best option for using LINQ with non-generic collections, both in terms of code…
Alex Angas
  • 59,219
  • 41
  • 137
  • 210
12
votes
2 answers

Why is HashSet.IsReadOnly explicit?

This var h = new HashSet(); var r = h.IsReadOnly; does not compile. I have to do var r = ((ICollection)h).IsReadOnly; why wasn't IsReadOnly implemented normally? (I'm not asking how, but why)
ripper234
  • 222,824
  • 274
  • 634
  • 905
12
votes
7 answers

How to hide some members of an interface

I would like to create a custom collection that implements ICollection. But I would like not to expose some memebers of ICollection like Clear method. How to achieve this?
nan
  • 19,595
  • 7
  • 48
  • 80
12
votes
5 answers

C# Clearing list before nulling

Today I have seen a piece of code that first seemed odd to me at first glance and made me reconsider. Here is a shortened version of the code: if(list != null){ list.Clear(); list = null; } My thought was, why not replace it simply by: list…
DereckM
  • 274
  • 2
  • 11
11
votes
1 answer

Microsoft Guidelines for Collections: Confused about several parts

I'm looking at Microsoft's Guidelines for Collections and I find a few parts hard to understand: X DO NOT use ArrayList or List in public APIs. Does this mean that I should avoid returning List altogether, or that I could return it as an…
lfk
  • 2,423
  • 6
  • 29
  • 46
11
votes
3 answers

Unable to cast object of type 'WhereEnumerableIterator`1' to type 'System.Collections.Generic.ICollection`1

I have the following code (please note that this is stripped down to the relevant part, the actual query is a lot more complex): public IQueryable GetMenus(DateTime lastUpdate) { ... result = GetAll().Where(m => lastUpdate <…
11
votes
5 answers

What is the easiest and most compact way to create a IEnumerable or ICollection?

So, many times we have a function that accepts an IEnumerable or ICollection as a parameter. In cases where we have single items, but no collection to hold them, we must create a collection before passing them to the function, like: T o1, o2,…
Ashley Simpson
  • 383
  • 1
  • 6
  • 16
10
votes
2 answers

Does AsQueryable() on ICollection really makes lazy execution?

I am using Entity Framework CodeFirst where I have used Parent Child relations using ICollection as public class Person { public string UserName { get;set} public ICollection Blogs { get; set;} } public class Blog { public int id {…
Emran Hussain
  • 11,551
  • 5
  • 41
  • 48
10
votes
3 answers

Does List.ToList() enumerate the collection

Through the travels in StackOverflow there are many usages of the ToList() extension method for an ICollection (or classes that derive from ICollection). I have pulled out the good ol' decompiler to have a look at how the ToList() extension…
Nico
  • 12,493
  • 5
  • 42
  • 62
8
votes
1 answer

Using IList, List, ICollection for 1 to n relationships in EF Core

What is the best practice to define a One-to-Many relationship in EF Core? Is it recommended to generally use lists, as these offer more functionalities? Then rather the interface IList instead of List, as it does not define the implementation? If…
raw
  • 99
  • 1
  • 5
8
votes
3 answers

converting linq query to icollection

I need to take the results of a query: var query = from m in db.SoilSamplingSubJobs where m.order_id == id select m; and prepare as an ICollection so that I can have something like ICollection subjobs at the moment I create…
bergin
  • 1,584
  • 2
  • 13
  • 21
8
votes
4 answers

T in class? AddRange ICollection?

I try to do static class, add to icollection but i got some issues i cant seem to overcome. that is how i get so i can pass a ICollection in the method? cause T is that say it can not be resolved. and then i wonder is there a way to do AddRange on…
Dejan.S
  • 18,571
  • 22
  • 69
  • 112
1
2
3
18 19