开发者

C# : interface : same method in 2 interfaces

开发者 https://www.devze.com 2023-03-18 23:23 出处:网络
I have 2 interfaces as, public interface I1 { string GetRandomString(); } public interface I2 { string GetRandomString();

I have 2 interfaces as,

    public interface I1
    {
       string GetRandomString();
    }

   public interface I2
   {
      string GetRandomString();
   }
开发者_StackOverflow中文版

and in a class, I have implanted both,

    public class ClassA : I1, I2
    {

      string I1.GetRandomString()
      {
         return "GetReport I1";
      }

      string I2.GetRandomString()
      {
          return "GetReport I1";
       }

    }

Now in main method I want to access , these interface method but not able to

    static void Main(string[] args)
    {
        var objClassA = new ClassA();
        objClassA.GetRandomString(); // not able to do this, comile time error ... 
    }

I know that , I am missing some basic OOPS stuff , just wanted to know that. any Help ?


If you will sometimes be wanting to use one interface and sometimes the other, it will likely be necessary to use a cast for at last one of them. If you control the type and can have one of the interface functions be available directly rather than as an explicit implementation, that will avoid the requirement for casting on that one. To avoid having to typecast either function, you should make them available under separate names within the object. Because in C# any method implementing anyInterface.Boz must be called Boz, the best bet approach is probably to have the implementations of IFoo.Boz and IBar.Boz simply call public methods called FooBoz and BarBoz, which could then be called "directly" without ambiguity.

Although casting to interfaces is inexpensive with classes, it can be expensive with structures. This cost may in some cases be avoided by using static methods like the following:

    public interface AliceFoo { void foo();};
    public interface BobFoo { void foo();};
    static void do_alice_foo<T>(ref T it) where T:AliceFoo
    {
        it.foo();
    }
    static void do_bob_foo<T>(ref T it) where T : BobFoo
    {
        it.foo();
    }

This approach allows either 'foo' method to be used without having to typecast anything.


The problem is that these functions are not member functions of MyClass, because they are defined and I1.GetRandomString as I2.GetRandomString. You can only call them on one of the interfaces:

    I1 objClassA = new ClassA();
    objClassA.GetRandomString();

or

    I2 objClassA = new ClassA();
    objClassA.GetRandomString();


You can cast the object to the type of interface that you require and then call the method.

if (objClassA is I1)
{
    ((I1)objClassA).GetRandomString();
}
if (objClassA is I2)
{
    ((I2)objClassA).GetRandomString();
}


You should cast your object to particular interface type.

static void Main(string[] args)
{
    var objClassA = new ClassA();

    I1 objClassAThreatedAsI1Interface = (I1)objClassA;
    objClassAThreatedAsI1Interface.GetRandomString();

    I2 objClassAThreatedAsI2Interface = (I2)objClassA;
    objClassAThreatedAsI2Interface.GetRandomString();
}


You've explicitly implemented the interfaces. So the method implementations belong to the interfaces, not the type you defined. As far as the compiler is concerned, the method does not exist for that type.

You need to explicitly cast your instance to the interface you want to call and call the method (as the others have shown).

Or better, choose one of the interfaces as the "main" interface and leave out the explicit implementation.

i.e.,

public class ClassA : I1, I2
{
    // make I1's implementation the "main" one
    public string GetRandomString()
    {
        return "GetReport I1";
    }

    // I2's implementation could only be called via a reference to an I2
    string I2.GetRandomString()
    {
        return "GetReport I2";
    }

}

Alternatively, just use a single implementation of the method (and leave out the explicit implementations).


Mark your two GetRandomString methods as public in ClassA. If you don't provide an access modifier, it defaults to private.


You need to cast your object to the interface that defines the version of the method that you wish to call.

If you just want a single version of the method to be defined in your class, remove the explicit definition of the interface (in other words, remove the I1. from the first definition of the function and entire I2.* method entirely)


using System;
namespace demo
{
    public interface I1
    {
        string GetRandomString();
    }

    public interface I2
    {
        string GetRandomString();
    }
    public class ClassA : I1, I2
    {
        
        public string GetRandomString()
        {
            return "GetReport I1";
        }

        
        string I2.GetRandomString()
        {
            return "GetReport I2";
        }
        public static void Main()
        {
            I1 objClassA = new ClassA();
            objClassA.GetRandomString();
           //I2 objClassA = new ClassA();
           // objClassA.GetRandomString();
        }

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消