Generic methods and type casting

Kevin Wallis

I read the following question (and I would solve it the same way as the given answer): Passing derived type as argument to abstract class

But why is it not able to find the value attribute from the derived class? even if I add a type cast it is not possible:

public abstract class baseClass
{
    public abstract float function<T>(T a, T b) where T:baseClass;
}

public class derived: baseClass
{
    public override float function<derived>(derived a, derived b)
    {
        // Here value is not found
        return a.value + b.value;
    }

    public float value;
}

Example with type cast is also not working (and the advice redundant type cast is shown):

public abstract class baseClass
{
    public abstract float function<T>(T a, T b) where T:baseClass;
}

public class derived: baseClass
{
    public override float function<derived>(derived a, derived b)
    {
        // Here value is not found even with type cast
        return ((derived)a).value + ((derived)b).value;
    }

    public float value;
}
Yuval Itzchakov

Because you're declaring the generic type parameter on the method. The compiler doesn't understand that this should be of the derived type. All it knows it that you've introduced a new generic type parameter.

What you want is called F-bound polymorphism, where the type parameter is of the implementing class, defined recursively:

public abstract class BaseClass<T> where T : BaseClass<T>
{
    public abstract float Function(T a, T b);
}

public class Derived : BaseClass<Derived>
{
    public override float Function(Derived a, Derived b)
    {
        return a.Value + b.Value;
    }

    public float Value { get; set; }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Casting generic type object

From Java

Generic primitive type with casting

From Java

Object to generic type casting

From Dev

Casting to a generic base type

From Dev

Casting to a generic type

From Dev

Dynamic casting to a generic type

From Dev

Type casting with a generic integer

From Dev

Casting classes in generic methods in MEF

From Dev

F#: Casting to generic type

From Java

Java: Casting Object to a generic type

From Dev

Java generics and casting a generic type

From Dev

Type casting in a generic swift function

From Dev

Implicit casting IEnumerable to generic type

From Dev

Casting consumer generic type to another type

From Java

Generic type casting vs parameterized type casting in Java

From Dev

Generic methods type inference

From Dev

Why does casting a generic return type to another generic type work?

From Java

How does casting this object to a generic type work?

From Dev

Casting generic function type arguments in c#

From Dev

C#: Casting from generic to value type

From Dev

Casting a variable of generic data type to String in Swift

From Dev

Casting object? to a generic type that may or may not be nullable

From Java

Warnings in Java when casting to a generic type

From Dev

Why generic to object type-casting is implicit?

From Dev

Casting to a generic of any type in C#

From Dev

casting generic-type list of maps

From Java

Generic casting / instance check / exclude type parameter

From Java

Check generic type before casting at runtime

From Dev

C# method generic return type casting