开发者

VB.net Overridable property not same as c# Virtual property?

Well its simple here you have my vb.net code:

Public Class Class1
    Public Overridable ReadOnly Property Name() As String
        Get
            Return Nothing
        End Get
    End Property
End Class

Public Class Class2
    Inherits Class1
    Public Overloads ReadOnly Pro开发者_JS百科perty Name() As String
        Get
            Return "Class2"
        End Get
    End Property
End Class

Module Module1
    Sub Main()
        Dim c2 As New Class2()
        Console.WriteLine(c2.Name)
        Dim c1 As Class1 = CType(c2, Class1)
        Console.WriteLine(c1.Name)
    End Sub
End Module

And here comes the c# code:

class Class1
{
    public virtual string Name
    {
        get
        {
            return null;
        }
    }
}

class Class2 : Class1
{
    public override string Name
    {
        get
        {
            return "Class2";
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Class2 c2 = new Class2();
        Console.WriteLine(c2.Name);
        Class1 c1 = (Class1)c2;
        Console.WriteLine(c1.Name);
    }
}

i did expekt them to do the same thing but guess what thay dont! C# Output

Class2

Class2

VB.NET output

Class2

{Nothing}

(It dosent print nothing it just prints an empty line) Why does vb.net go for the base class implementation when its overriden?


Should it be overrides and not overloads?

Public Overrides ReadOnly Property Name() As String
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜