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

后端 未结 21 1831
渐次进展
渐次进展 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:26

    I'm surprised that C#'s unsafe code has not been mentioned yet. This is not allowed in VB.NET.

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

    Handles and WithEvents keywords for automatic wiring of EventHandlers.

    Private Sub btnOKClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
    End Sub
    
    0 讨论(0)
  • 2020-12-05 06:27

    There were some useful articles in Visual Studio magazine back in Jan 2008.

    • What C# developers should know about VB
    • What VB developers should know about C#
    0 讨论(0)
  • 2020-12-05 06:27

    In C# you can declare a property in an interface as having a 'get', and then implement it in a class with a get and set.

    public interface IFoo {
      string Bar {get;}
    }
    
    public class Foo : IFoo {
      public string Bar {get; set;}
    }
    

    In VB, the equivalent to declating a property with a get would be to declare it ReadOnly. You can't then make the implementation writable.

    Public Interface IFoo 
    
      ReadOnly Property Bar() As String
    
    End Interface
    
    Public Class Foo 
       Implements IFoo
    
      Public Property Bar() As String Implements IFoo.Bar  'Compile error here'
    
    End Class
    

    I find this to be a severe limitation of VB. Quite often I want to define an Interface that allows other code only to be able to read a property, but I need a public setter in the implemented class, for use by the persistor.

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

    In VB you can implement an interface with a method of any name - i.e. a method "Class.A" can implement interface method "Interface.B".

    In C#, you would have to introduce an extra level of indirection to achieve this - an explicit interface implementation that calls "Class.A".

    This is mainly noticeable when you want "Class.A" to be protected and/or virtual (explicit interface implementations are neither); if it was just "private" you'd probably just leave it as the explicit interface implementation.

    C#:

    interface IFoo {
        void B();
    }
    class Foo : IFoo { 
        void IFoo.B() {A();} // <====  extra method here
        protected virtual void A() {}
    }
    

    VB:

    Interface IFoo
        Sub B()
    End Interface
    Class Foo
        Implements IFoo
        Protected Overridable Sub A() Implements IFoo.B
        End Sub
    End Class
    

    In the IL, VB does this mapping directly (which is fine; it is not necessary for an implementing method to share a name).

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

    VB and C# have different interpretations of what "protected" means.

    Here's an explanation copied below:

    The default constructor for WebControl is protected.

    VB and C# have different interpretations of what "protected" means.

    In VB, you can access a protected member of a class from any method in any type that derives from the class.

    That is, VB allows this code to compile:

    class Base
        protected m_x as integer
    end class
    
    class Derived1
        inherits Base
        public sub Foo(other as Base)
            other.m_x = 2
        end sub
    end class
    
    class Derived2
        inherits Base
    end class
    

    Because a "Derived1" is a base, it can access protected members of "other", which is also a base.

    C# takes a different point of view. It doesn't allow the "sideways" access that VB does. It says that access to protected members can be made via "this" or any object of the same type as the class that contains the method.

    Because "Foo" here is defined in "Derived1", C# will only allows "Foo" to access "Base" members from a "Derived1" instance. It's possible for "other" to be something that is not a "Derived1" (it could, for example, be a "Derived2"), and so it does not allow access to "m_x".

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