简体   繁体   中英

Can a method implement multiple interface methods?

These are two implementations of interface methods and as you can see, they do the same thing in this case .

public override IList<IDatastoreRequestParameter> GetReadByOwnerIdParameters(int id) {
    return new List<IDatastoreRequestParameter> {
        new DatastoreRequestParameter("@CustomerId", id)
    };
}

public override IList<IDatastoreRequestParameter> GetDeleteByOwnerIdParameters(int id) {
    return new List<IDatastoreRequestParameter> {
        new DatastoreRequestParameter("@CustomerId", id)
    };
}

However, as we use this interface everywhere, sometimes they are the same but they can also have different parameters returned in the implementation. I know I can just create a private method to return the same thing, I'm just curious to see if it's possible. My gut tells me no, but SO is smarter than my gut, so here I am.

You cannot make one method and tell the compiler/platform to use it when calling two different methods. As you said, you can create a private method that will handle the case and call that, but you do have to implement the methods in the interface separately.

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