简体   繁体   中英

Can generic interface expose methods w same name but diff # signature params?

I am attempting to decode this fairly complex program.

There is a generic interface called IInterface_1. It exposes a method, "MethodName" which has one parameter in the method signature.

 public interface IInterface_1<S> where S : class, "ISomeClass", new()
{
    S MethodName(IEnumerable<Ilistname> Name);
}

I expect that whichever generic class or interface that inherits IInterface_1 must provide an implementation or path to implement the method "MethodName". As shown, MethodName has one parameter in the method signature.

However, elsewhere in the program the same method name is exposed by another interface "IInterface_2", where there are not one but two parameters in the method signature. In both cases the constraints are identical with the exception that IIinterface_2 inherits from IInterface_1.

public interface IInterface_2<S> : IInterface_1 where S : class, "ISomeClass", new()
{
    S MethodName(IEnumerable<Ilistname> Name, ISomethingElse Name_2);
}

IInterface_2 intherits from IInterface_1, but IInterface_2 and IInterface_1 expose the same method but with a different number of parameters. From what I know about interfaces, the above would violate the interface "contract", but this program works fine. What have I missed?

Thank You Tom

Benefit from having that interface composition is that when some of your classes implements IInterface_1 it needs implement just S MethodName(IEnumerable<Ilistname> Name);

In case it's implementing IInterface_2 it needs to implement both

S MethodName(IEnumerable<Ilistname> Name);
S MethodName(IEnumerable<Ilistname> Name, ISomethingElse Name_2);

And so when you're using object abstracted by IInterface_2 somewher in your app you should be able call both methods.

It also isn't agains any C# syntax rule. Interfaces you've described have different names. Both mentioned methods have same names but different set of arguments.

Morover even if they would have same parameters it may be valid C# code. Try following program and check what's written into console.

    public interface IBasicInterface
    {
        int Test();
    }

    public interface IAdvancedInterface : IBasicInterface
    {
        int Test();
    }

    public class AdvancedClass : IAdvancedInterface
    {
        int IBasicInterface.Test()
        {
            return 1;
        }

        int IAdvancedInterface.Test()
        {
            return 2;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            AdvancedClass tester = new AdvancedClass();
            Console.WriteLine(((IAdvancedInterface)tester).Test()); // returns 2
            Console.WriteLine(((IBasicInterface)tester).Test());    // returns 1
            Console.ReadLine();
        }
    }

But it's also true that usages of this design are rare I'd say.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM