Niladri Sekhar Dutta
Is it possible to implement two interfaces in the same class if the interfaces have same method signature ,name,same number of methods etc.? If yes How?
By Niladri Sekhar Dutta in C# on Jun 05 2017
  • mohammed yaseen
    May, 2021 2

    Yes we can. We have to explicitly implement the interfaces in such cases. We can do that by using interface name and it's method name in the class..Interface I1{void m(); }Interface I2{void m(); }Class A : I1, I2 {public void I1.m(){ }public void I2.m(){ } }

    • 1
  • Binod Kumar
    Jun, 2019 28

    interface A { void Hello(); } interface B { void Hello(); } class Test : A, B { public void Hello() { Console.WriteLine("Hello to all"); } } public class interfacetest { public static void Main() { Test Obj1 = new Test(); Obj1.Hello(); } }

    • 1
  • kajal rane
    Apr, 2019 7

    Yes.IT Will be possible by using explicit interface name with method name

    • 1
  • Ronak patel
    Sep, 2018 14

    Yes Possible can use explicit implementation.

    • 0
  • Jitendra Sharma
    Aug, 2018 20

    by the explicitly interface implementation

    • 0
  • Joy K
    Jun, 2018 7

    Yes, by implementing the concept of explicit implementation of the interface where we specify InterfaceName.MethodName to avoid ambiguity.

    • 0
  • Pravinkumar Birajdar
    Apr, 2018 10

    yes, use explicit implementation of interfaces

    • 0
  • Akbar Mulla
    Jan, 2018 8

    Yes. It is called Explicit Interface Implementation.

    • 0
  • bebo bebo
    Nov, 2017 25

    yes you can implement method by name of interface first

    • 0
  • Niladri Sekhar Dutta
    Jun, 2017 5

    We have use the "Explicit Interface implementation" feature of C# to achieve this. For example // Declare the English units interface: interface IEnglishDimensions {float Length();float Width(); } // Declare the metric units interface: interface IMetricDimensions {float Length();float Width(); } //You can implement in the class like below class Box : IEnglishDimensions, IMetricDimensions {float lengthInches;float widthInches;public Box(float length, float width) {lengthInches = length;widthInches = width;} // Explicitly implement the members of IEnglishDimensions:float IEnglishDimensions.Length() {return lengthInches;}float IEnglishDimensions.Width() {return widthInches; } // Explicitly implement the members of IMetricDimensions:float IMetricDimensions.Length() {return lengthInches * 2.54f;}float IMetricDimensions.Width() {return widthInches * 2.54f;} }Warning : An interface member that is explicitly implemented cannot be accessed from a class instance: You have to cast it to interface type to call the method.Check this MSDN link for the full example :https://msdn.microsoft.com/en-us/library/aa288461(v=vs.71).aspx

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS