Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Equivalent of Java's GenericType<?>

Tags:

c#

generics

I'm working with some generics in C#. I have an abstract generic class:

public abstract class BaseClass<T> where T : Foo { }

and the type parameter is specified when other classes inherit from the base class.

I'm also trying to write some code for another abstract class that needs to store one of these base classes, but it won't know the parameter of the base class (and it doesn't need to know). In Java I could simple write:

protected BaseClass<?> myBaseClass;

But in C# it insists I give it a type parameter. If I declare it as:

protected BaseClass<Foo> myBaseClass;

I cannot assign any parametrized values of BaseClass to it.

Is there a work around to achieve the effect of BaseClass<?> in C#? Obviously there are ways to restructure my code to avoid the need, such as parameterizing all of the classes that use BaseClass as well, but this would be less than ideal. Any help would be appreciated!

like image 641
thomas88wp Avatar asked Jan 15 '14 16:01

thomas88wp


1 Answers

To match Java's behavior somewhat in C#, it is common to put an additional non-generic interface in the inheritance hierarchy.

public interface IBaseClass
{
    Foo GetValue();

    void SetValue(Foo value);
}

public abstract class BaseClass<T> : IBaseClass
    where T : Foo
{
    public T GetValue<T>()
    { /* ... */ }

    public void SetValue<T>(T value)
    { /* ... */ }

    Foo IBaseClass.GetValue()    // Explicit interface method implementation
    {
        return (Foo)GetValue<T>();
    }

    void IBaseClass.SetValue(Foo value)    // Explicit interface method impl.
    {
        SetValue<T>((T)value);
    }
}

And then use IBaseClass where you need BaseClass<?>:

IBaseClass myClass;
Foo f = myClass.GetValue();
like image 122
Daniel A.A. Pelsmaeker Avatar answered Sep 30 '22 14:09

Daniel A.A. Pelsmaeker