Implementing ICollection

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I'm writing a Roster class for our company's training program, and I'd like the class to implement the ICollection interface using our Employee class as a template:

Visual Basic:
public class Roster : ICollection<Employee>

When I punched this into VS2008, Intellisense gave me the options under ICollection to Implement Interface 'ICollection<Employee>' and Explicitly Implement Interface 'ICollection<Employee>'.

Poo-doo! The ICollection interface is something I've never gotten a chance to work with, so I don't really know what the difference is. I just picked the simpler looking one, Implement Interface 'ICollection<Employee>'.

VS constructed several of the required abstract class signitures (is there a term for that?) for me filled with the default "throw new NotImplementedException();" line in each routine, and I proceeded to fill these values using my private List<Employee> empList object.

Later, I noticed that Intellisense still gave me the option to Explicitly Implement Interface 'ICollection<Employee>'. Since I can't seem to find any articles either in my C# books or online that tell me what the differences are, I went ahead and picked that one too.

Now in my code, I've got what looks like duplicates:
Visual Basic:
public void Add(Employee item)
{ // Implement interface 'ICollection<Employee>'
  empList.Add(item);
}
// And now I have a new private member that I don't know
// what to do with below:
void ICollection<Employee>.Add(Employee item)
{ // Explicitly Implement interface 'ICollection<Employee>'
  throw new NotImplementedException();
}
Help!
Where can I find something that shows me how to implement all of these methods?
If a base class to implement from would help, here's what I have:
Visual Basic:
[Serializable()]
public class Roster
{
  List<Employee> empList;
  public Roster()
  {
    empList = new List<Employee>();
  }
}
 
[Serializable()]
public class Employee
{
  public Employee()
  {
    IdNumber = 0;
  }
  public Employee(int idNumber)
  {
    IdNumber = idNumber;
  }
  // Number that links to the Employee's ID Number in the database
  public int IdNumber { get; set; }
}
With this simple stub, could someone show me how to implement ICollection on the Roster class?

Thanks,
Joe
 
I'd say you had it right the first time. If you implicitly imlement an interface, C# just assumes that the methods with the right names and signatures map to the interface methods. This is the most common approach.

To implement an interface explicitly, you name your methods InterfaceName.MethodName. The methods will be "anonymous" (you can't access them directly from an object of the declaring type, and must cast to the interface type to call the methods). Explicitly implemented interface methods take precedence over implicitly implemented interface methods (if you define both, the implicit implementation is ignored).

The option to implement an interface is per-method. The only reasons you would generally implement an interface method explicitly is if you want to:
  • Give the methods different names than the interface calls for (Close versus Dispose)
  • Change the signature of the method (usually to something more specific or strongly typed)
  • Hide or abstract the interface implementation in the implementing class.
  • Two implemented interfaces have conflicting method names


To implement collection classes, I tend to inherit the List<T> class, rather than wrap it. If I need to modify behavior, I have an IList<T> wrapper class ready to include in my project and tweak as needed. This way I very rarely have to manually implement IList or ICollection. If you don't inherit an ICollection class then you will need to implement it yourself.
 
Back
Top