Software Engineering : This category relates to interesting computer related stuff that I am researching or reading about. Most of it is in the area of .NET technologies which is the focus of most of my computer related time at the moment.
Updated: 9/21/2004; 3:35:46 PM.

 








Subscribe to "Software Engineering" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

Subscribe To
Mark's Weblog

 
 

Monday, September 01, 2003

Implementing an Interface Multiple Times in One Class Using Generics
Google Search It

With the C# generics implementation that is currently under proposal is will be possible to declare generic interfaces, not just generic classes.  This is really cool as it avoids the ambiguity associated with returning an object such as in the System.Collections.IEnumerable interface.  In place of the non-generic interface we would use the generic System.Collections.Generics.IEnuerable version:

public interface IEnumerable<T>

{

  IEnumerator<T> GetEnumerator();

}

Furthermore, it will be possible to implement an interface multiple times within one class.  For example, it should be possible to do the following:

public interface IContainer<ItemType>

{

  IEnumerable<ItemType> Items();

}

 

public class Person: IContainer<Address>,
  IContainer<Phone>, IContainer<Email>

{

  ICollection<Address> IContainer<Address>.Items()

  {

    ...

  }

 

  ICollection<Phone> IContainer<Phone>.Items()

  {

    ...

  }

 

  ICollection<Email> IContainer<Email>.Items()

  {

    ...

  }

}

If email and phone were both strings this wouldn't be possible as the interface on both would be IContainer<string> but assuming each container holds a different type this works. 

Interesting?  I like it....

(By the way, technically, the Person class does not implement the same interface multiple times because IContainer<Address> and IContainer<Phone> are different interfaces.  You cannot even cast between them.)


10:25:39 PM   []    comment []

© Copyright 2004 Mark Michaelis.



 


September 2003
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
Aug   Oct


Recent Posts