Search code examples
overridingtype-conversionmethod-signature

C# - Override <T> method signature with ClassName?


Is there a way to override an abstract class's method signature which uses <T> with a ClassName so I can pass an object by reference without recasting it?

For example, I have a bunch of Object Managers. I want them all to contain a .Save(Object) method which will perform the appropriate save action based on the object state (Insert, Update, Delete, etc).

I was trying to create a base class which contains these methods:

protected virtual bool Update<T>(ref T _object) where T : ObjectBase
{
    throw new NotImplementedException();
}

public virtual bool Save<T>(ref T _object) where T : ObjectBase
{ 
    // Figure out which action to take based on _object's state and execute it
}

And I wanted my inherited classes to define the methods using something like this:

public override bool Update<Consumer>(ref Consumer _object)
{
    return _service.UpdateConsumer(ref _object);
}

My problem is that I can't specify that <T> will now be <Consumer>, and by keeping it at <T> I can't pass it by ref


Solution

  • Instead of making the methods themselves generic, you should make the entire base class generic.

    For example:

    public abstract class ObjectManager<T> where T : ObjectBase {
        protected abstract bool Update(T obj);
    }
    

    Each concrete ObjectManager should inherit ObjectManager of the type that it manages, like this:

    public class ConsumerManager : ObjectManager<Consumer> {
        protected override bool Update(Consumer obj) {
            ...
        }
    }
    

    Note, by the way, that your parameters should almost definitely not be passed ref.
    You only need to ref keyword if you want to change the caller's variable to refer to a different instance.
    For more information, see here.