简体   繁体   中英

No suitable method to override in C#

I have the following method in my code, which takes a Guid as a parameter. I want to override this method with one that takes a string as a parameter:

public class SendEmailApi: ISendEmailApi
{

     public async Task<EmailTemplate> GetTemplateByIdAsync(Guid templateId)
     {
         var response = await _emailClient.RequestAsync(EmailClient.Method.GET, urlPath: $"templates/{templateId}");

         return JsonConvert.DeserializeObject<EmailTemplate>(await response.Body.ReadAsStringAsync());
     }
}

However when I try to implement, I get the error that there is no suitable method to override.

public async override Task<EmailTemplate> GetTemplateByIdAsync(string templateId)
{
     var response = await _emailClient.RequestAsync(EmailClient.Method.GET, urlPath: $"templates/{templateId}");

     return JsonConvert.DeserializeObject<EmailTemplate>(await response.Body.ReadAsStringAsync());
}      

The interface looks like this:

public interface ISendEmailApi
{
    Task<EmailTemplate> GetTemplateByIdAsync(Guid templateId);
}

I am relatively new to C# and .NET and any advice would be appreciated - I am sure I am missing something obvious? Thanks

The other method you want is not an override , override means you're overriding a virtual method in the base class. And you have no base class.

Remove the override keyword in your function and it will work as you want.

The error is correct. In order to override an existing method it has to have the exact same signature as the method it is overriding. Since your method takes in a string it can't override a method that takes in a Guid. Instead I think you want to overload the method ( Overloading and overriding .)

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