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
7
votes
3 answers

Need Duplicates Allowed In SortedCollection (C#, 2.0)

I have a project that I'm working on that requires changing a 'BaseSortedCollection' class to allow duplicates. The class currently implements IEnumerable, IDisposable, ICollection, and ISerializable. The 'BaseSortedCollection' stores Items that…
Chris
7
votes
2 answers

Why lock on Collection.SyncRoot instead of just lock the collection?

I'm trying to understand the point of the syncroot in ICollection. Why not just lock the collection? lock(myCollection) { //do stuff to myCollection } vs lock(myCollection.SyncRoot) { //do stuff to myCollection }
richard
  • 12,263
  • 23
  • 95
  • 151
7
votes
2 answers

ICollection vs ICollection- Ambiguity between ICollection.Count and ICollection.Count

Note: This is similar, but not quite the same as this other question I've implemented an IBusinessCollection interface. It dervies from both ICollection, and the old-busted non-generic ICollection. I'd prefer to just dump the old busted…
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
6
votes
2 answers

Code contracts warnings when implementing ICollection with backing collection

I've got this code: public class MyCollection : ICollection { private readonly ICollection _inner = new Collection(); public void Add(string item) { _inner.Add(item); } // <-- CodeContracts: ensures…
Allrameest
  • 4,364
  • 2
  • 34
  • 50
6
votes
3 answers

Can I hide my ICollection fields when I have a one-to-many mapping in EF4 code-only?

My domain classes that have one-to-many mappings generally take the following form (untested code): public Customer Customer { // Public methods. public Order AddOrder(Order order) { _orders.Add(order); } public Order…
dommer
  • 19,610
  • 14
  • 75
  • 137
6
votes
3 answers

Is instantiating a Queue using {a,b,c} possible in C#?

Is it possible to do that in C#? Queue helperStrings = {"right", "left", "up", "down"}; or do I have to produce an array first for that?
xeophin
  • 123
  • 6
6
votes
1 answer

Extension methods for both ICollection and IReadOnlyCollection

I want to write an extension method (e.g. .IsEmpty()) for both ICollection and IReadonlyCollection interfaces: public static bool IsEmpty(this IReadOnlyCollection collection) { return collection == null || collection.Count == 0; } public…
user1067514
  • 494
  • 3
  • 15
6
votes
2 answers

ASP MVC3 - BeginCollectionItem returning nulls

I am trying to use Steve Sanderson's blog post regarding binding collection items to a model. However, I'm seeing some weird behavior that I can't find an answer for in the post or other discussions. In my model BankListMaster, I have an…
NealR
  • 10,189
  • 61
  • 159
  • 299
5
votes
1 answer

Using ASP.NET MVC 3 with Razor, what's the most effective way to add an ICollection to a Create view?

I'm using Entity Framework Code First to generated my database, so I have an object defined like the following: public class Band { public int Id { get; set; } [Required(ErrorMessage = "You must enter a name of this band.")] public…
James Skemp
  • 8,018
  • 9
  • 64
  • 107
5
votes
1 answer

C# Get properties from SettingsPropertyCollection

I have profile provider in my web.config .......
Dmytro Leonenko
  • 1,443
  • 5
  • 19
  • 30
5
votes
1 answer

Array doesn't implement ICollection but is assignable

Look at this insane piece of code: ICollection list = new[] {1, 2, 3}; list.Add(4); // NotSupportedException: Collection was of a fixed size. I'm not wondering about the exception! I'm wondering that a simple array could be assigned to…
Marcel
  • 1,002
  • 2
  • 16
  • 37
5
votes
2 answers

ICollection / ICollection ambiguity problem

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(this ICollection obj) { return ((obj !=…
Mose
  • 1,781
  • 3
  • 16
  • 35
5
votes
4 answers

Setting the Value of an ICollection Property in C#

My class inherits an interface so I need to have the emailaddress property in the Person class. My question is what is the best way to get the property and set the property public class Contact : IPerson, ILocation { ... [MaxLength(256)] …
utd1878
  • 95
  • 1
  • 9
4
votes
5 answers

Split a ICollection with a delimiter sequence

This is for C# 3.5 I have ICollection that I'm trying to split into separate ICollections where the delimiter is a sequence. For example ICollection input = new byte[] { 234, 12, 12, 23, 11, 32, 23, 11 123, 32 }; ICollection delimiter =…
Corey S
  • 45
  • 1
  • 7
4
votes
1 answer

Can't convert ICollection to IEnumerable

I am building the expression tree: {x => x.principal_info.First().agent_for_property_info} It works as expected. In fact, I need to convert it to IEnumerable instead of ICollection as you can see here. Here is the method that works: public…
AlexZholob
  • 317
  • 1
  • 16
1 2
3
18 19