« Previous entry | Next entry » Browse > Snippets

Skip to comments (10) [C#] Explicit interfaces
Posted by Matthijs on Mar 07 2006 @ 23:14  :: 5511 unique visits

When using C# it could be that you would like to implement two or more interfaces with the same method, but that you would still like to implement both of them (and make them do different stuff).

This is really simple to do:
CODE: CSHARP
interface IA
{
  void DoSomething();
}

interface IB
{
  void DoSomething();
}

class TestClass : IA, IB
{
  //Explicitly implement the DoSomething for the IA interface
  void IA.DoSomething()
  {
    Console.WriteLine("IA");
  }

  void DoSomething()
  {
    Console.WriteLine("The rest"); //Kinda like the "default"
  }
}


This way, depending on the type, the appropriate function will be called!

CODE: CSHARP
TestClass tc = new TestClass();

tc.DoSomething(); //This wil print "The rest"

(tc as IA).DoSomething(); // Output: "IA"

(tc as IB).DoSomething(); // Output: "The rest"
 


Just a small tip that could come in handy!

10 comments posted so far
Add your own »

1. On Mar 07 2006 @ 23:30 Niek wrote:

This is also handy if you'd like to force others to call methods on the interface, instead of calling them on the derived class.

2. On Mar 09 2006 @ 00:04 guest wrote:

for crying out LOUD.

If you wanted C++, you should have sticked to it...

3. On Mar 09 2006 @ 09:00 Matthijs wrote:

lol :)

4. On Mar 09 2006 @ 23:57 Gauthier wrote:

#post: This is the base of interfaces, strong typing and clear design with C#, but I'm "amazed" how much people don't know this particular feature (explicit interface implementation).

What I wonder is (about the previous c# post) var keyword will allow:

var member = new SpecificMember() as IMember;

#1: I gave a quick & dirty introduction on interfaces and .net (and their origins) to my collaborators, and this was the main study case (how to use interfaces and weighting the pros & cons enforcement of interface typed variables to access the interface members).
Is it in your habits (in your daily job) to implement interfaces like that? when I showcased the "feature" much people has fears and was frowned? they asked about productivity (even some corps use vb.net with strict off...!!).

I think it's a good way to initiate the use of interfaces (enforce to declare variables with interface instead of concrete type...) and to enhance the design/readability of the code.

#2: I'm currently studing other languages than c#; could you explain me how you define interfaces in c++ (I thought it was only possible via the use of abstract classes but I'm still a newbie in c++)?

Thanks

5. On Mar 16 2006 @ 21:45 l0ne wrote:

Gauthier: yes, it will be allowed. As the resulting type of the expression is IMember, the compiler will expand it as:

IMember member = new SpecificMember() as IMember;

6. On Jul 06 2007 @ 12:22 guest wrote:

simply superb

7. On Feb 04 2009 @ 00:36 xiang wrote:

Just for today Idofus kamas will try to live  acheter des kamasnot my whole life  dofus kamasonce. I can do kamas dofussomething for twelvebuy kamas hours that appall dofus kamasme if I had tokamas dofus prepare them for buy kamasa lifetime. Just for dofus kamastoday I will be happy.kamas dofusThis presupposes, however,
buy kamasto be true what dofus kamasAbraham Lincoln said kamas dofusthat "Most people arebuy kamas as happy as it appearsacheter des kamas to be their opinion." acheter dofusJust for today I will<acheter des kamas adapt what is and notacheter dofus try to solve everythingacheter des kamas with my own wishes. Iacheter dofuswill do my "lucky" as it comes.
acheter des kamasJust for today I will acheter dofustry, in my opinion. I want to study. I want to learn something useful.

8. On Apr 14 2009 @ 13:29 guest wrote:

Find information about tiffany ,gucci ,chanel and other jewelry online shopping at online shopping ,
jewelry,craft,antique,daily news online collection at Online Collector ,Tiffany Jewelry including Tiffany Necklaces,Tiffany Rings, and tiffany bracelets…
Guide To Buy Tiffany Product ,   fashion jewelry provide,Tiffany,Oxette,Swarovski,CHANEL Jewelry Information
Find the discount gucci shoes <br]



Louis Vuitton is luxury gifts, French fashion, the replica Louis Vuitton Handbag is woman best friend.Monogram Groom.
Offers Discount Louis Vuitton handbags and Louis Vuitton bags and all other designer handbags,free global fast shipping,low price and top quality.Monogram Jokes,Monogram Suede cheap Louis Vuitton
Louis Vuitton.

Looking For Gucci Shoes ? Gucci Store provide gucci Mens shoes,gucci Womens shoes
Wonderful Gucci shoes sale Gucci men's shoes and Gucci women's shoes at discount Gucci Shoes prices.
cheap gucci Shoes
Gucci Shoes and gucci clothing Spring - Summer 2009, Prada Shoes and prada clothing from the Latest Collection 2009 and Dolce Gabbana Clothing 2009
Gucci Loafers
Gucci Sneakers

Louis Vuitton Handbags
UGGs
Louis Vuitton Handbags
Gucci Shoes
Louis Vuitton
UGG Boots
Louis Vuitton Handbags
gucci shoes
Monogram Groom
Discount Louis Vuitton
UGG Boots

Add a new comment

Name:
Password: (leave empty for anonymous comment)
 
View formatting tags Comment: