0

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

sayah imad
  • 1,507
  • 3
  • 16
  • 24
Jordan1993
  • 864
  • 1
  • 10
  • 28
  • 2
    You don´t need to override the method. As you inherit from an interface just add a new method to the interface `Task GetTemplateByIdAsync(string templateId);` – Martin E Jun 16 '20 at 14:09
  • 6
    What you want is a new overload. [override](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/override) is used "to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event" – Broots Waymb Jun 16 '20 at 14:09

2 Answers2

3

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.

Blindy
  • 65,249
  • 10
  • 91
  • 131
0

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.)

schwechel
  • 305
  • 3
  • 10