Overloading, vb.net vs C #

Source: Internet
Author: User
The implementation of vb.net is different from the implementation of C # in many aspects. Today we will compare it with Overload.

Everyone may be familiar with C #. Let me list a piece of code:

Namespace OverloaddingCS

{

Public class Test

{

Public static void Main ()

{

Derived d = new Derived ();

D. WriteLine (10 );

D. WriteLine ("11 ");

DS. WriteLine (10.5 );

}

}


Class Base

{

Public void WriteLine (String AnyString)

{

Console. WriteLine (AnyString + "called Base: String ");

}


Public void WriteLine (int AnyInteger)

{

Console. WriteLine (AnyInteger. ToString () + "called Base: Integer ");

}

}


Class Derived: Base

{

Public void WriteLine (double AnyDouble)

{

Console. WriteLine (AnyDouble. ToString () + "called Derived: double ");

}

}

}

The answer is simple. If you are familiar with c ++

10 called Derived: double

11 called Base: String

10.5 called Derived: double

If I translate similar code into vb.net, note that it is the original translation.

Class TestClass Test

Public Shared Sub Main ()

Dim objMyTest As New Derived

With objMyTest

. WriteLine (10)

. WriteLine ("11 ")

. WriteLine (10.5)

End

Console. ReadLine ()

End Sub

End Class

Class BaseClass Base

Public Sub WriteLine () Sub WriteLine (ByVal AnyString As String)

Console. WriteLine (AnyString + "called Base: String ")

End Sub


Public Sub WriteLine () Sub WriteLine (ByVal AnyInteger As Integer)

Console. WriteLine (AnyInteger. ToString + "called Base: Integer ")

End Sub

End Class


Class DerivedClass Derived: Inherits Base

Public Overloads Sub WriteLine () Sub WriteLine (ByVal AnyDouble As Double)

Console. WriteLine (AnyDouble. ToString + "called Derived: Double ")

End Sub

End Class

The beginning of the Code is the same. I directly published the results, starting with a different result from C #.

10 called Base: Integer

11 called Base: String

10.5 called Derived: Double

The results are unexpected, And the beginning is also reasonable.

I will not explain it. The key is to remind you that when you have C ++ special VB. NET, do not take it for granted that the processing method of VB. NET is the same as that of C.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.