Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

is generic method override supported?

Discussion in 'Scripting' started by tavovallejo, Jun 18, 2016.

  1. tavovallejo

    tavovallejo

    Joined:
    Nov 22, 2014
    Posts:
    13
    This code works in .NET 3.5, but not in Unity 5.3.4f1
    Code (CSharp):
    1. public class ClassA
    2. {
    3.     /// <summary>
    4.     /// works fine
    5.     /// </summary>
    6.     public virtual T2 Example<T1, T2>(T1 val) where T2 : T1
    7.     {
    8.         return (T2)val;
    9.     }
    10. }
    11. public class ClassB : ClassA
    12. {
    13.     /// <summary>
    14.     /// error here
    15.     /// </summary>
    16.     public override T2 Example<T1, T2>(T1 val)
    17.     {
    18.         return base.Example<T1, T2>(val);
    19.     }
    20. }
    I get : error CS0309: The type `T2' must be convertible to `T1' in order to use it as parameter `T2' in the generic type or method `ClassA.Example<T1,T2>(T1)'
     
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    818
    make sure the override version is also using the "where T2 : T1" contraint.