Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Failed to properly override default interface method

Discussion in 'Scripting' started by geniusz, Sep 16, 2023.

  1. geniusz

    geniusz

    Joined:
    Nov 21, 2014
    Posts:
    38
    Code (CSharp):
    1. public interface ITest { int Prop => 10; }
    2. public abstract class CTest : ITest {}
    3. public class CTestChild : CTest { public virtual int Prop => 20; }
    4.  
    5. ITest a = new CTestChild();
    6. Debug.Log(a.Prop);
    I'm using Unity 2021.3.15, the result shown in the above code is 10, while my expected result is 20.

    In a normal dotnet runtime, the result is 20.

    Can you help me confirm that this issue still exists in the new version? I would like to know if it is necessary to upgrade Unity.
     
  2. tsukimi

    tsukimi

    Joined:
    Dec 10, 2014
    Posts:
    51
    I tried some patterns in 2022.2.11, here's the result:
    Code (CSharp):
    1. // Yours, output: 10
    2. public abstract class CTest : ITest { }
    3. public class CTestChild : CTest { public virtual int Prop => 20; }
    4.  
    5. // Move the prop to parent class, output: 20
    6. public abstract class CTest : ITest { public virtual int Prop => 20; }
    7. public class CTestChild : CTest { }
    8.  
    9. // Child also inherit ITest interface, output: 20
    10. public abstract class CTest : ITest { }
    11. public class CTestChild : CTest, ITest { public virtual int Prop => 20; }
    (By the way, recently Unity is trying to update to CoreCLR, maybe it will fix the problem; but it might take 1~2 years)
     
  3. geniusz

    geniusz

    Joined:
    Nov 21, 2014
    Posts:
    38
    Thank you very much for your help