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
  • 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