Is it possible to make an interface method accept different classes C#?

Hi I was wondering if there is any nice way without an overkill to have an interface method, that could take in for example 3 different models? I'm looking for something like this:

 public interface IOperations 
    {
        void AddTranslation(TranslationLocationEnum Location, T model)               
    }

 public class ApplicationOne : IOperations
   {
        public void AddTranslation(TranslationLocationEnum Location, ClassType1 model)     
   }

 public class ApplicationTwo : IOperations
   {
        public void AddTranslation(TranslationLocationEnum Location, ClassType2 model)     
   }

Thank you

Upvotes: 0

Views: 77

Answers (1)

Saket Choubey
Saket Choubey

Reputation: 916

public interface IOperations<T> where T : class
{
    void AddTranslation(TranslationLocationEnum Location, T model);
}

public class ApplicationOne : IOperations<ApplicationOne>
{
    public void AddTranslation(TranslationLocationEnum Location, ApplicationOne model)
    {
        throw new NotImplementedException();
    }
}

public class ApplicationTwo : IOperations<ApplicationTwo>
{
    public void AddTranslation(TranslationLocationEnum Location, ApplicationTwo model)
    {
        throw new NotImplementedException();
    }
}

Use generic interface and pass type at the time of inheriting the interface. Hope this helps.

Upvotes: 3

Related Questions