Explicitly Implementing Interface in ASP.NET


Explicit implementation of interface can be helpful when class implements interfaces having save method signatures

Explicit implementation of interfaces in C# can be helpful when class implements interfaces having save method signatures


interface IAgency

{

void AddUpdate();

}

interface IPackage

{

void AddUpdate();

}

class Travel : IAgency, IPackage

{

public void AddUpdate()

{

}

}


By Implementing Interfaces explicitly we can avoid the above problem


interface IAgency

{

void AddUpdate();

}

interface IPackage

{

void AddUpdate();

}

class Travel : IAgency,IPackage

{



void IAgency.AddUpdate()

{

throw new NotImplementedException();

}

void IPackage.AddUpdate()

{

throw new NotImplementedException();

}

}


Travel obj = new Travel();

//obj.AddUpdate(); // Compiler error.

IAgency objAgency = (IAgency)obj;

objAgency.AddUpdate(); // Calls IAgency.AddUpdate on Travel

IPackage objPackage = (IPackage)obj;

objPackage.AddUpdate(); // Calls IPackage.AddUpdate on Travel




Reference: http://buddha-smiles.blogspot.com/2011/04/explicitly-implementing-interface.html


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: