I'm working in c# trying to implement 2 classes that use the same interface. Both classes only require one method each. However the two methods need to be of different types, taking different parameters. I would really like to be able to inherit one single method from the interface and overwrite it in each class as this would be nice and neat (the interface is necessary for the program functionality (working with dependency injection)) Unfortunately both classes are also necessary as the email sender class will be used repeatedly in other programs.
I have found a solution which ALMOST fits my problem, it is: Implementing methods from an interface but with different parameters
The difference that I have is that each of my two classes should only call one method.
Here is my code as it stands (having tried to implement the generics in the previous solution):
interface IEmailSender<T>
{
T DoEmail();
}
public class EmailGenerator : IEmailSender<MailMessage>
{
public override MailMessage DoEmail(string emailAddress)
{
MailMessage message = new MailMessage();
//code to generate email message
return message;
}
}
public class EmailSender : IEmailSender<bool>
{
public override bool DoEmail(MailMessage message)
{
//code to send email
return true;
}
}
Ideally I'd like my email sender method to be void but I don't mind it returning a boolean if it has to have a return value.
Hope that makes sense, I'm literally just trying to be neat here because currently in order to make it work, I would need to have two methods in my interface and have each class call an empty method.
Thanks in advance,