What is allowed in Visual Basic that's prohibited in C# (or vice versa)?

后端 未结 21 1828
渐次进展
渐次进展 2020-12-05 06:07

This is code-related as in what the compiler will allow you to do in one language, but not allow you to do in another language (e.g. optional parameters in

相关标签:
21条回答
  • 2020-12-05 06:18

    Off the top of my head (pre 4.0):

    VB language "features" not supported in C#:

    • Optional Parameters
    • Late Binding
    • Case insensativity

    I'm sure there's more. Your question might get better answers if you ask for specific examples of where each language excels. VB is a currently better than C# when interacting with COM. This is because COM is much less of a headache when optional parameters are available, and when you don't have to bind to the (often unknown type) at compile time.

    C# on the other hand, is preferable by many when writing complex logic because of its type safety (in that you can't bypass static typing) and its conciseness.

    In the end, the languages are mostly equivalent, since they only differ on the fringes. Functionally, they are equally capable.

    EDIT

    To be clear, I'm not implying that VB doesn't allow static typing... simply that C# doesn't [yet] allow you to bypass static typing. This makes C# a more attractive candidate for certain types of architectures. In the 4.0 C# language spec, you can bypass static typing, but you do it by defining a block of dynamic code, not by declaring the entire file "not strict," which makes it more deliberate and targeted.

    0 讨论(0)
  • 2020-12-05 06:18

    One of the overlooked or simply misunderstood features of the VB language is calling a function which has a ByRef parameter. Most languages support only a single method of passing parameters by reference: that is the scenarios directly supported by the CLR.

    The CLR has a lot of restrictions on the type of values it supports for ByRef parameters and these restrictions get in the way of VB’s goal to be a flexible language. Hence the compiler goes to great lengths to be flexible and support multiple avenues of ByRef passing, much beyond what the CLR natively allows.

    C# 4 now supports two versions of reference passing. In addition to the one available since 1.0 the ref modifier is now optional when making an interop call to a COM object.

    0 讨论(0)
  • 2020-12-05 06:20

    VB has optional parameters on functions.

    C# will only get these with C# 4.0

    0 讨论(0)
  • 2020-12-05 06:20

    As Chris Dunaway mentioned, VB.NET has Modules which allow you to define functions and data.

    VB.NET has the VB6 syntax for linking to methods in DLLs. For example:

    Declare SetSuspendState Lib "powrprof" As Function (byval hibernate as Int32, byval forceCritical as Int32, byval disableWakeEvent) as Int32
    

    (Although that actual declaration might have to be Marshalled)


    0 讨论(0)
  • 2020-12-05 06:22

    VB allows nonvirtual calls to virtual instance methods (call in IL), whereas C# only allows virtual calls (callvirt in IL). Consider the following code:

    Class Base
        Public Overridable Sub Foo()
            Console.WriteLine("Base")
        End Sub
    
        Public Sub InvokeFoo()
            Me.Foo()
            MyClass.Foo()
        End Sub
    End Class
    
    Class Derived : Inherits Base
        Public Overrides Sub Foo()
            Console.WriteLine("Derived")
        End Sub
    End Class
    
    Dim d As Base = New Derived()
    d.InvokeFoo()
    

    The output is:

    Derived
    Base
    

    That's not possible in C# (without resorting to Reflection.Emit).

    0 讨论(0)
  • 2020-12-05 06:22

    The new autoproperties in C# have not been done for VB.NET yet.

    0 讨论(0)
提交回复
热议问题