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
438
votes
10 answers

Why use ICollection and not IEnumerable or List on many-many/one-many relationships?

I see this a lot in tutorials, with navigation properties as ICollection. Is this a mandatory requirement for Entity Framework? Can I use IEnumerable? What's the main purpose of using ICollection instead of IEnumerable or even List?
Jan Carlo Viray
  • 11,856
  • 11
  • 42
  • 63
166
votes
3 answers

ICollection Vs List in Entity Framework

I only watched a few webcasts before I went head first in to designing a few Entity Framework applications. I really didn't read that much documentation and I feel like I am suffering for it now. I have been using List in my classes, and it has…
wil
  • 2,019
  • 2
  • 15
  • 14
63
votes
5 answers

ICollection - Get single value

What is the best way to get a value from a ICollection? We know the Collection is empty apart from that.
Udo Weber
61
votes
4 answers

Difference between IEnumerable and IEnumerable?

What is the difference between IEnumerable and IEnumerable? I've seen many framework classes implementing both these interfaces, therefore I would like to know what advantages one get by implementing both? Please have a look how they've been…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
38
votes
3 answers

Using a list as a data source for DataGridView

I've extracted the setting names and their respective values out of a configuration file into an ordered dictionary. The dictionary contains keys and values which are of the ICollection class. I want to bind that data and display it in a…
tf.rz
  • 1,347
  • 6
  • 18
  • 47
32
votes
9 answers

Does an ICollection have an order?

Following the rules that a public APIs should never return a list, i'm blinding converting all code that returned lists, to return ICollection instead: public IList CommaSeparate(String value) {...} becomes public ICollection
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
28
votes
3 answers

Custom Collection using IEnumerable vs ICollection vs IList

I need to design my own custom GenericCollection class. Now i have plenty of options to derive it using IEnumerable, ICollection, and IList, where later offers some added functionalities. I am little confused that if i go with IEnumerable i might…
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
24
votes
5 answers

Add to an ICollection

I am currently writing a C# project and I need to do unit testing for the project. For one of the methods that I need to unit test I make use of an ICollection which is normally populated from the selected items of a list box. When I create a unit…
Boardy
  • 35,417
  • 104
  • 256
  • 447
21
votes
7 answers

Why ICollection index does not work when instantiated?

When we declare a parameter as ICollection and instantiated the object as List, why we can't retrive the indexes? i.e. ICollection Products = new List(); Products.Add(new ProductDTO(1,"Pen")); Products.Add(new…
demokritos
  • 1,416
  • 2
  • 12
  • 34
19
votes
2 answers

How to turn ICollection into IReadOnlyCollection?

When I have a variable of ICollection in C#, I cannot pass it to a function that expects an IReadOnlyCollection: public void Foo() { ICollection data = new List(); // Bar(data); // Not allowed: Cannot implicitly cast…
LWChris
  • 3,320
  • 1
  • 22
  • 39
16
votes
2 answers

Unit-testing IList with CollectionAssert

The MSTest framework has a CollectionAssert that accepts ICollections. My method returns an IList. Apparently a list is not a collection.. Are there ways to make my IList an ICollection?
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
16
votes
3 answers

Does System.Array Really Implement ICollection?

According to MSDN docs, System.Array implements ICollection, yet System.Array does not provide a Count property (of course you can always use the LINQ Count() extension method, but there is no property with this name). How can this be? Isn't Count…
Paul Keister
  • 12,851
  • 5
  • 46
  • 75
15
votes
3 answers

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

Before I even ask, let me get the obvious answer out of the way: The ICollection interface includes a Remove method to remove an arbitrary element, which Queue and Stack can't really support (since they can only remove "end" elements). OK,…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
15
votes
5 answers

What is the real advantage of returning ICollection instead of a List?

I've read a couple of blog post mentioning that for public APIs we should always return ICollection (or IEnumerable) instead of List. What is the real advantage of returning ICollection instead of a List? Thanks! Duplicate: What is the difference…
Martin
  • 39,309
  • 62
  • 192
  • 278
15
votes
2 answers

How to Clear() all elements from Entity Framework ICollection?

I have problems removing all elements from a collection in entity framework using Clear() Consider the often used example with Blogs and Posts. public class Blog { public int Id {get; set;} public string Name {get; set;} public virtual…
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
1
2 3
18 19