VB.NET and C# Comparison

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank McCown

    VB.NET and C# Comparison

    I've created a quick reference comparison of C# and
    VB.NET that you might find useful. I couldn't find
    anything like it on the web and so I made my own.


    rison.html

    It doesn't cover every area of the languages, just those
    features most frequently used.

    I'd be thankful for any suggestions you may have on ways
    to improve the comparisons.

    Thanks,
    Frank
  • Tom Shelton

    #2
    Re: VB.NET and C# Comparison

    In article <043701c39f2e$5 8a27660$a301280 a@phx.gbl>, Frank McCown wrote:[color=blue]
    > I've created a quick reference comparison of C# and
    > VB.NET that you might find useful. I couldn't find
    > anything like it on the web and so I made my own.
    >
    > http://www.harding.edu/USER/fmccown/...t_csharp_compa
    > rison.html
    >
    > It doesn't cover every area of the languages, just those
    > features most frequently used.
    >
    > I'd be thankful for any suggestions you may have on ways
    > to improve the comparisons.
    >
    > Thanks,
    > Frank[/color]

    I'd like to point out Frank that you have a technical inaccuracy on your
    enum section. The word stop can be used as an enum value. You just
    have to do it like this:

    Enum Action
    Start
    [Stop]
    Rewind
    Forward
    End Enum

    Dim a As Action = Action.Stop

    Also, further down in the For Each statement. In 1.1, you can declare
    the loop variable inside the For Each statement:

    For Each s As String In names
    ...
    Next

    That also applies to the For Loop

    For i As Integer = 0 To nums.Length
    ...
    Next

    Another point (sorry, pointing these out as I go):

    VB.NET's try catch block can have conditions (when clause):

    Dim x As Integer = 10
    Dim y As Integer = 0
    Dim z As Integer

    Try
    z = x / y
    Catch ex As Exception When y = 0
    ....
    Finally
    ....
    End Try

    you can't do that in C# :)

    In the properties - you have foo.Size++ in the VB.NET side. That should
    be foo.Size += 1

    On the events - you might want to point out that RaiseEvent will not
    throw an exception if the event is a null reference. In c# you have to
    check that before you invoke the delegate.

    On console, IO - on the C# side. You don't have to use the
    Convert.ToChar method to convert an int to a char. You can just cast
    it:

    Console.WriteLi ne("{0}", (char) 65);


    Other then those, it looks pretty good. I hope you don't take this post
    the wrong way, just pointing out a few little things.

    --
    Tom Shelton
    MVP [Visual Basic]

    Comment

    • Fergus Cooney

      #3
      Re: VB.NET and C# Comparison

      Thanks Tom

      Catch ex As Exception When y = 0
      I never knew that one.

      Hi Frank,

      Like Tom I think your work is highly worthy and I'm also am just applying
      my polishing rag. I'm assuming that was what you were hoping for anyway, as
      well as giving <us> a useful resource. It will come in handy when we get the
      'I have some C# snippets can you...' queries.

      So - DreamWeaver at the ready?


      In VB you can have IIf instead of C#'s ()?:
      It's common but I consider it poor practice to explicitly assign true or
      false when the condition itself gives the value
      teen = (age >= 13 And age <= 19)

      I would use something more like:
      greeting = IIf (age >= 13 And age <= 19, "Uugh!", "Hello")


      The line continuation can be used to good effect to split an If but omit
      the End If

      If SomethingOrOthe rRatherLongWind edToDefine Then _
      DoSomethingCons equential (AndWith, Lots, OfArgs)


      With ' Resize the array, keeping the existing values (optional)
      I'd have both versions separately so that there's no ambiguity about
      what's optional.


      In 'Pass by value ... you might want to note that ByVal is the default and
      can be omitted.

      Dim a = 1, b = 1, c As Integer
      Console.WriteLi ne("{0} {1} {2}", a, b, c) ' 1 2 5

      b should be 2 for your output but c - initialised by default will be
      zero - giving '1 2 0


      Again, with the ByVal - ByVal ParamArray can be just ParamArray.


      With /* C# doesn't support optional arguments/parameters, I'd actually
      show the overload explicitly (and introduce that word)


      'Depricated' is spelt with an 'e'. Lol. When I looked it up in the
      dictionary I used an 'a' and the dictionary put <me> right, too. ;-)


      Given that your audience is students, they may appreciate this example:
      Dim Up As New Exception ("Uuurgh - can't handle this.")
      Throw Up


      Interface IAlarmClock
      Inherits ISuperAlarm

      These should be reversed I believe. Unfortunately that mean that Batman
      inherits from something Super, but SuperAlarm inherits from something
      ordinary. This is correct, codingwise, but conceptually liable to mislead
      (small risk but present). I'd lose one of the 'Super's. I like the Batman one,
      so - Interface IAlarmClock : Inherits IClock. This is one of the few places
      that I use the : syntax to put the two lines together.


      Classes can have constructors:
      Public Shared Sub New

      You might also like to point out Private constructors for Shared-only
      classes.


      .Name = "SpamMan"
      .PowerLevel = 3
      SpamMan, like VirusMan and WormMan, is a force for evil and therefore has
      negative powers. ;-)


      hero.Rest() ' Calling Shared method
      ' or
      SuperHero.Rest( )

      SuperHero is the Shared method.


      Dim hero2 As SuperHero = hero ' Makes shallow copy
      Copy is very misleading especially when used with 'shallow' as this latter
      word is properly used with 'clone'. The assignment is duplicated reference but
      not a copy of the object (as you show in the following code).

      You might, therefore, want to introduce MemberwiseClone (shallow) and
      Clone (deep) in contrast to the duplicated reference. All objects have
      MemberWiseClone , Clone needs to be explicitly implemented.


      Dim stu2 As StudentRecord = stu ' Makes deep copy
      This one makes a shallow copy. A deep copy is where any referenced objects
      are also copied - and perhaps (if implemented that way) references to their
      ref... etc.


      In Character constants, you might like to demonstrate embedded double
      quotes.
      Gossip = "So she said ""with a kipper"". No joking - that's what she
      said..."

      There's also:
      Dim InitialLetter As Char = "F"c
      If InitialLetter >= "A"c And InitialLetter <= "Z"c Then ...


      You've got
      age = Convert.ToInt32 (Console.ReadLi ne())
      There's still
      age = Val (Console.ReadLi ne())
      which is much more forgiving, especially for unchecked console input.


      Phew. I bit off more than I thought there!!

      I'd suggest posting this to these groups as well. They must get lots of
      conversion queries too.
      languages.cshar p
      dotnet.general
      dotnet.academic

      Regards,
      Fergus


      Comment

      • Frank McCown

        #4
        Re: VB.NET and C# Comparison

        Thank you Tom and Fergus for your excellent comments.
        I'll make some corrections very soon.

        Frank

        Comment

        • Frank McCown

          #5
          Re: VB.NET and C# Comparison

          > Dim stu2 As StudentRecord = stu ' Makes deep copy[color=blue]
          > This one makes a shallow copy. A deep copy is where[/color]
          any referenced objects[color=blue]
          >are also copied - and perhaps (if implemented that way)[/color]
          references to their[color=blue]
          >ref... etc.
          >
          >
          >Regards,
          >Fergus
          >
          >[/color]

          I REALLY appreciate the input. I do have a question
          about copying structures though... I read threw the MSDN
          VB.NET documentation on structures and saw this:

          "When you assign one structure variable to another... the
          current values of all the variable members are copied to
          the new structure."

          So isn't the assigning of a structure to another actually
          performing a deep copy?

          Thanks,
          Frank

          Comment

          • Fergus Cooney

            #6
            Re: VB.NET and C# Comparison

            Hi Frank,

            Structure assignments are shallow copies, ie. only the ValueType fields
            and strings. Strings - despite being a reference type - are a special case as
            they are immutable, meaning that string data can only be pointed to by a
            single variable/field/whatever at a time.

            The documentation is correct. The 'value' of an object reference is the
            address of the object not the object itself. So only the address gets copied.

            So, any objects <referenced> by the structure (this includes arrays) will
            not be copied (but the reference will). Nested structures, however, being
            ValueTypes, <will> be copied (though not, of course, <their> referenced
            objects)..

            Here's an example:

            Structure Nest
            Public oMother As clsBird 'Reference copied
            Public aoEggs() As structEgg 'Reference Copied
            Public oCuckooEgg As structEgg 'Copied
            Public dCreated As DateTime 'Copied
            Public iHoursTaken As Integer 'Copied
            Public sMaterials As String 'Copied
            End Structure

            I briefly mentioned 'shallow' versus 'deep' in the other posting so it'll
            make more sense when you find that bit again. But here's some more about it.

            All the above was about 'shallow' copying - valuetypes, references but not
            actual objects. 'Deep' copying therefore goes beyond the object in question
            (whether structure or class) and copies the linked objects too. 'Deep copying'
            is a phrase associated with Clone and Copy methods. Each class that implements
            these has a choice of whether to do it as a shallow copy or a deep copy. If
            shallow is chosen, then MemberwiseClone can be returned. If it's a deep copy
            though, the referenced objects must be asked to clone themselves as well. Each
            of these, in turn, has the same choice and may respond with a shallow or deep
            copy of itself.

            In the following conversation Jay talks more about Cloning.


            Regards,
            Fergus


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: VB.NET and C# Comparison

              Fergus,[color=blue]
              > Structure assignments are shallow copies, ie. only the ValueType[/color]
              fields[color=blue]
              > and strings. Strings - despite being a reference type - are a special case[/color]
              as[color=blue]
              > they are immutable, meaning that string data can only be pointed to by a
              > single variable/field/whatever at a time.[/color]
              Is this what you wanted to say?

              Strings are immutable in that you cannot change their contents, Strings are
              not special in this regard per se as you are free to make any other class or
              structure immutable.

              Remember Immutable means not changeable.
              [color=blue]
              > meaning that string data can only be pointed to by a
              > single variable/field/whatever at a time.[/color]

              That's not correct, any number of string variables can point to the same
              instance of a string object (aka "string data")!

              Dim s1 As String = "Hello world"
              Dim s2 As String = s1

              There is only one string object in the above sample, both variables are
              referencing it.

              Hope this helps
              Jay

              "Fergus Cooney" <filter@post.co m> wrote in message
              news:%23GY%23N7 LoDHA.2188@TK2M SFTNGP11.phx.gb l...[color=blue]
              > Hi Frank,
              >
              > Structure assignments are shallow copies, ie. only the ValueType[/color]
              fields[color=blue]
              > and strings. Strings - despite being a reference type - are a special case[/color]
              as[color=blue]
              > they are immutable, meaning that string data can only be pointed to by a
              > single variable/field/whatever at a time.
              >
              > The documentation is correct. The 'value' of an object reference is[/color]
              the[color=blue]
              > address of the object not the object itself. So only the address gets[/color]
              copied.[color=blue]
              >
              > So, any objects <referenced> by the structure (this includes arrays)[/color]
              will[color=blue]
              > not be copied (but the reference will). Nested structures, however, being
              > ValueTypes, <will> be copied (though not, of course, <their> referenced
              > objects)..
              >
              > Here's an example:
              >
              > Structure Nest
              > Public oMother As clsBird 'Reference copied
              > Public aoEggs() As structEgg 'Reference Copied
              > Public oCuckooEgg As structEgg 'Copied
              > Public dCreated As DateTime 'Copied
              > Public iHoursTaken As Integer 'Copied
              > Public sMaterials As String 'Copied
              > End Structure
              >
              > I briefly mentioned 'shallow' versus 'deep' in the other posting so[/color]
              it'll[color=blue]
              > make more sense when you find that bit again. But here's some more about[/color]
              it.[color=blue]
              >
              > All the above was about 'shallow' copying - valuetypes, references but[/color]
              not[color=blue]
              > actual objects. 'Deep' copying therefore goes beyond the object in[/color]
              question[color=blue]
              > (whether structure or class) and copies the linked objects too. 'Deep[/color]
              copying'[color=blue]
              > is a phrase associated with Clone and Copy methods. Each class that[/color]
              implements[color=blue]
              > these has a choice of whether to do it as a shallow copy or a deep copy.[/color]
              If[color=blue]
              > shallow is chosen, then MemberwiseClone can be returned. If it's a deep[/color]
              copy[color=blue]
              > though, the referenced objects must be asked to clone themselves as well.[/color]
              Each[color=blue]
              > of these, in turn, has the same choice and may respond with a shallow or[/color]
              deep[color=blue]
              > copy of itself.
              >
              > In the following conversation Jay talks more about Cloning.
              > http://tinyurl.com/ta40
              >
              > Regards,
              > Fergus
              >
              >[/color]


              Comment

              • Fergus Cooney

                #8
                Re: VB.NET and C# Comparison

                Hi Jay,

                I wanted to get a new slant on 'immutable' but I chose an incorrect
                'slant'. And, having picked up that ball, I succesfully ran down the field
                with it to score an own-goal. Oops! ;-)

                I suppose I could have qualified it by saying

                || meaning that string data can
                effectively
                || only be pointed to by a
                || single whatever at a time

                in the sense that as soon as you do anything to a string, separation
                occurs.

                But that runs the risk, as you pointed out, of being misleading on the
                term 'immutable'

                Thanks, Jay, a useful correction.

                Cheers,
                Fergus


                Comment

                • Tom Leylan

                  #9
                  Re: VB.NET and C# Comparison

                  "Fergus Cooney" <filter@post.co m> wrote...

                  Immutable strings are passe'. I think the language needs uni-mutable (these
                  could be "muted" once), semi-mutable (you can change part of the string but
                  not the entire string and de-mutable (these could revert to some previous
                  version of the string.) Frankly the one that's missing is auto-mutable.
                  These just change periodically on their own.




                  Comment

                  • Fergus Cooney

                    #10
                    Re: VB.NET and C# Comparison

                    Hi Tom,

                    ROFL - clever, too. :-))

                    Regards,
                    Fergus


                    Comment

                    • Fergus Cooney

                      #11
                      Re: VB.NET and C# Comparison

                      Hi Tom,

                      || Frankly the one that's missing is auto-mutable.
                      || These just change periodically on their own

                      Apparently this has already been implemented for Bound TextBoxes on a
                      Tabbed Page. No announcement has been made, as people will be demanding it in
                      other contexts, but the silent roll-out has caused misunderstandin g, with some
                      wondering "why this no work".

                      Regards,
                      Fergus


                      Comment

                      Working...