0

I have a question. I have a class and interface for it, so in class I have 3 methods that looks similar and those are:

    public CurrentsFlagAnalysis GetCurrentsFlag(DateTime startDateTime, DateTime endDateTime)
    {
        //some code
    }

    CurrentsFlagAnalysis ICurrentService<CurrentsFlagAnalysis>.GetCurrentsFlag(DateTime startDateTime, DateTime endDateTime, byte id)
    {
        //some code
    }

    List<CurrentsFlagAnalysis> ICurrentService<List<CurrentsFlagAnalysis>>.GetCurrentsFlag(DateTime startDateTime, DateTime endDateTime, byte id)
    {
        //some cone
    }

And interface looks like:

public interface ICurrentService <out TCurrent>
{
    TCurrent GetCurrentsFlag(DateTime startDateTime, DateTime endDateTime, byte id);

    CurrentsFlagAnalysis GetCurrentsFlag(DateTime startDateTime, DateTime endDateTime);
}

The idea is to use those two methods with same name and same parameters but different return type similar to overload, but i came across the problem when i call this method for example:

    public Task<List<CurrentsFlagAnalysis>> GetCurrentsFlagAsync(DateTime startDateTime, DateTime endDateTime, byte id)
    {
        return Task.Run(() => GetCurrentsFlag(startDateTime, endDateTime, id));
    }

From Compile time:

error CS1501: No overload for method 'GetCurrentsFlag' takes 3 arguments

and Visual studio sends me a message of ambiguous invocation and possible argument null exception;

I am getting ambiguous invocation implement error, I know i should use some sort of explicit implementation, but don't know hot to bite it.

And Another thing is this thing safe, should I just rename method and forget that idea.

2
  • Please could you provide a minimal reproducible example with the exact error message? Commented May 10, 2018 at 8:19
  • 1
    Cast this to get access to the explicit implementation: ((ICurrentService<CurrentsFlagAnalysis>)this).GetCurrentsFlag(...). Commented May 10, 2018 at 8:30

1 Answer 1

2

Even from within the same class, once you've made methods be explicit interface implementations, you have to call them via a reference to the interface:

return Task.Run(() => ((ICurrentService<CurrentsFlagAnalysis>)this).GetCurrentsFlag(
                                                               startDateTime, 
                                                               endDateTime, 
                                                               id));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.