What are the advantages of C# over VB.NET and vice versa?

Published 11 March 04 01:59 AM

The choice between C# and VB.NET is largely one of subjective preference. Some people like C#'s terse syntax, others like VB.NET's natural language, case-insensitive approach. Both have access to the same framework libraries. Both will perform largely equivalently (with a few small differences which are unlikely to affect most people, assuming VB.NET is used with Option Strict on). Learning the .NET framework itself is a much bigger issue than learning either of the languages, and it's perfectly possible to become fluent in both - so don't worry too much about which to plump for. There are, however, a few actual differences which may affect your decision:

VB.NET Advantages
  • Support for optional parameters - very handy for some COM interoperability
  • Support for late binding with Option Strict off - type safety at compile time goes out of the window, but legacy libraries which don't have strongly typed interfaces become easier to use.
  • Support for named indexers (aka properties with parameters).
  • Various legacy VB functions (provided in the Microsoft.VisualBasic namespace, and can be used by other languages with a reference to the Microsoft.VisualBasic.dll). Many of these can be harmful to performance if used unwisely, however, and many people believe they should be avoided for the most part.
  • The with construct: it's a matter of debate as to whether this is an advantage or not, but it's certainly a difference.
  • Simpler (in expression - perhaps more complicated in understanding) event handling, where a method can declare that it handles an event, rather than the handler having to be set up in code.
  • The ability to implement interfaces with methods of different names. (Arguably this makes it harder to find the implementation of an interface, however.)
  • Catch ... When ... clauses, which allow exceptions to be filtered based on runtime expressions rather than just by type.
  • The VB.NET part of Visual Studio .NET compiles your code in the background. While this is considered an advantage for small projects, people creating very large projects have found that the IDE slows down considerably as the project gets larger.
C# Advantages
  • XML documentation generated from source code comments. (This is coming in VB.NET with Whidbey (the code name for the next version of Visual Studio and .NET), and there are tools which will do it with existing VB.NET code already.)
  • Operator overloading - again, coming to VB.NET in Whidbey.
  • Language support for unsigned types (you can use them from VB.NET, but they aren't in the language itself). Again, support for these is coming to VB.NET in Whidbey.
  • The using statement, which makes unmanaged resource disposal simple.
  • Explicit interface implementation, where an interface which is already implemented in a base class can be reimplemented separately in a derived class. Arguably this makes the class harder to understand, in the same way that member hiding normally does.
  • Unsafe code. This allows pointer arithmetic etc, and can improve performance in some situations. However, it is not to be used lightly, as a lot of the normal safety of C# is lost (as the name implies). Note that unsafe code is still managed code, i.e. it is compiled to IL, JITted, and run within the CLR.

Despite the fact that the above list appears to favour VB.NET (if you don't mind waiting for Whidbey), many people prefer C#'s terse syntax enough to make them use C# instead.

[Author: Jon Skeet]

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Martin Spedding said on March 11, 2004 2:12 AM:
Hi,

after writing a C# application that automated both Word and Outlook, I found the lack of optional parameters a real pain. Any change of this functionality being added to the language or providing more intelligent wrappers for the COM objects so that I do not always have to explicitly provide the optional parameters.
# John Cavnar-Johnson said on March 11, 2004 3:37 AM:
You forgot to mention the Catch ... When syntax which C# inexplicably lacks.
# Me said on March 11, 2004 4:45 AM:
... and the C# "using" statement.
# Rai Umair said on March 11, 2004 5:37 AM:
Read my article http://www.codeproject.com/dotnet/dnlp.asp
# Stefano Demiliani said on March 11, 2004 6:57 AM:
I like the event management of VB.NET (Handles clause)
# Dave said on March 11, 2004 8:01 AM:
I wonder how often the C#-only feature of being able to reimplement an interface in a derived class which was already implemented by a base class is used?
# Jon Skeet said on March 12, 2004 12:07 AM:
Added some of the points above.
# Mark Sargent said on March 12, 2004 1:06 AM:
Note this link: http://weblogs.asp.net/rpooley/posts/21818.aspx

We have large VB projects that are unusable in VB.Net due to the size of the project. If your solution is likely to include a large number of classes, I would strongly recommend c#
# Thomas Tomiczek said on March 12, 2004 1:16 AM:
One omission:

VB.NET is CLS only. This has certain implications. I, personally, love unsigned data types. Though you can always use the structs.
# Jon Skeet said on March 12, 2004 2:22 AM:
I'm not sure I'd consider the background compilation of VB.NET a <i>language</i> consideration, but I'll note it anyway. (For small projects, I'm sure many would consider it an advantage.)

On the "CLS-only" front, I've already noted that C# has unsigned data types in the language and VB.NET doesn't - anything more you'd like me to include there?
# Panos Theofanopoulos said on March 12, 2004 2:04 PM:
> anything more you'd like me to include there?

unsafe code and DllImport.

About "Explicit interface implementation" how in VB can you implement a private interface (that also uses private types) in a public class without it ?
# Jon Skeet said on March 15, 2004 1:35 AM:
I can't believe I left unsafe code out - bizarre. I'll fix that now.

I'm not a VB.NET programmer, but is there really no equivalent of DllImport in VB.NET?

Not sure what you mean about the private interfaces business, I'm afraid... could you give an example?
# Panos Theofanopoulos said on March 15, 2004 9:48 AM:
DllImport : VB and C# produce different signatures

http://weblogs.asp.net/adam_nathan/archive/2003/04/25/56643.aspx

If you're a VB.NET programmer, note that when you use Declare statements, the compiler emits PInvoke signatures that automatically set SetLastError to true. (C# forces you to opt-in because having the CLR track the last error is slightly slower than not tracking it.)

private interfaces :
internal interface IFoo {
void Foo();
}
internal interface IBar {
void Bar(Ifoo foo);
}

public class FooBar : IBar {
// in C# i will explicit implement it, in VB ?
}
# Jon Skeet said on March 16, 2004 2:08 AM:
I'll look more carefully at the DllImport business, but I think the private interface thing works. In VB.NET it's:

Imports System

Friend Interface IFoo
Sub Foo
End Interface

Friend Interface IBar
Sub Bar(ByVal foo as IFoo)
End Interface

Class Test
Implements IBar

Overridable Sub Bar(ByVal foo as IFoo) Implements IBar.Bar
End Sub

End Class


Given this, I should probably remove explicit interface implementation from the C# list... unfortunately I don't know VB.NET well enough to be absolutely sure.
# khurram said on March 17, 2004 11:25 AM:
please reply me as soon as possible about the
advantages of vb.net over vb6 by comparison.
# Jon Skeet said on March 18, 2004 4:17 AM:
I don't think a discussion of VB6 vs VB.NET is really relevant to a C# FAQ.
# Meenakshi said on March 18, 2004 9:06 PM:
Pls. let me know
# Dennis' blog said on March 20, 2004 5:50 AM:
# Hector Minaya said on April 23, 2004 2:12 PM:
How about any advantages over speed, I've read the entire fact and everything discused here has been about syntax.

Does anyone have any facts about any speed advantages.
# Troy Stauffer said on May 7, 2004 11:30 AM:
If written right, there is not speed difference. (i.e. if you use Directcast rather that CType and if you use Option Strict On and etc.) then it is almost 99% identical IL code.
# Anson Goldade said on May 17, 2004 10:14 AM:
One difference that has caused us a large amount of pain is VB not understanding operators denfined on some types. Take the System.Data.SqlTypes namespace for example. Even though there is an implicit operator from Int32 to SqlInt32, you can't use the following syntax in VB.NET

Dim i As SqlInt32 = 10

Instead you have to do either of the following

Dim i as SqlInt32 = New SqlInt32(10)
or
Dim i as SqlInt32 = SqlInt32.op_Implicit(10)

I'm not sure how or why VB understands operators for some types in the System namespace but not others, but it is a real pain.

Here's a couple of others to think about:
1. Shared members are exposed in derived classes. For example, the static Object.Equals(objA, objB) will show up for every class as a shared member even though it's only defined in Object. Can make it pretty confusing as to who is actually doing the work. Especially if someone decides to shadow Object's implementation.
2. No field attribute modifier. Try serializing an object where a form has a reference to an event on that object and you'll see what I mean.
3. No escape character in strings. Console.WriteLine("The actor was quoted as saying ""now that's wierd""")
4. How do you write an IIF in VB.NET where both return values are not evaluated even though only one will be returned. Try this and see what you get.

Dim o As Object
Console.WriteLine(IIf(True, "Good", o.ToString()))

</gripe>

Anson
# Jashkar said on June 2, 2004 1:43 AM:
OUT parameter will support by C#
# Jimmy plr said on June 9, 2004 12:31 AM:
Comparing vb to vb.net the main difference is the dotnet version is purely object oriented.
# AQ said on June 11, 2004 12:07 PM:
Assuming everything else (almost) equal, which one is faster to learn and get a handle on for a new starter?
# Luis Carlos said on June 11, 2004 1:45 PM:
I think that another advantage of C# over VB.NET is that C# is adhered to OOP theorical terminology like abstract, virtual, static, etc, while VB.NET use a propietary jerga (mustInherits, overridable, shared, etc).

That is important when you read theory or use UML tools like Visio or Rational Rose.
# Daniel said on June 21, 2004 6:44 AM:
How much faster is looping through unsafe arrays in C# compared to VB?
# Daniel said on June 21, 2004 6:47 AM:
In other words, how do unsafe arrays in C# compare to the always managed arrays of VB?
# Richard Clark said on August 12, 2004 9:43 PM:
# 河端善博の .TEXT でウェブログ said on January 25, 2005 2:07 AM:
C#とVB.NET の面白い比較投稿
# .NET - The place of happiness for everyone. said on March 24, 2006 3:13 AM:
Op de bijeenkomst van gisteren is door Frans Bouma en Maurice de Beijer strijd geleverd over de vraag...
# Indika’s Web Log » VB6 to C#.NET - and why not VB.NET said on May 2, 2007 4:01 PM:

PingBack from http://www.indika.info/?p=4

# The Demise of C# - Page 5 | keyongtech said on January 22, 2009 3:46 AM:

PingBack from http://www.keyongtech.com/510075-the-demise-of-c/5

# A Manager&#8217;s Retrospective on the C# versus VB.NET decision &laquo; Software++ said on April 19, 2009 8:09 PM:

PingBack from http://softwareplusplus.wordpress.com/2009/04/19/a-managers-retrospective-on-the-c-versus-vbnet-decision/

# Joep said on September 20, 2009 8:47 AM:

You forgot to mention the "yield" keyword in C#. Missing yield in VB has made my life into hell now our projectmanager wisely made us convert to VB.

# Panagiotis Roditakis said on September 28, 2009 3:56 AM:

I our days most people use the .NET Framework to write 'Enterpise Level - Data Driven Applications' that rarely require advanced programming features and techniques. So when an 'upgrading' developer (new to VS) has to deside which language is the most appropriate, the one that has the most intellisense support and is most readable to the human eye will make the difference when code extends to some ten of thousands of lines. Only past experience, training and current working environments create the exception to the above rule.

# Kelly said on November 25, 2009 7:59 PM:

I just recently made the change from VB to C# in <a href="http://www.juggle.com/microsoft">Microsoft</a> .Net.  I came from a Java background so C# really isn't too much different as far as syntax, but after learning VB I liked it a lot better.  I think VB is easier to ready and flows out a little better while coding.  I was hesitant to make the switch to C#, but it hasn't been too bad.

There are definitely advantages to both languages.  I have found that Visual Studio seems to be a little better integrated with VB, but C# offers nice ways to consolidate code.

It's always going to be an ongoing debate, but in the end a programming language is a programming language.  Syntax you can learn, but it's the logical thinking behind it that counts.

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker