VB.NET or C#: choose your weapon
Takeaway: The .NET Framework allows you to use your preferred language while offering most of the same features in VB.NET and C#. This article explores some of the subtle differences between VB.NET and C#.
The debate over which language to use—VB.NET or C#—has been going strong since .NET was first introduced. While you may use other languages, these two are by far the most popular. Each language uses the same .NET code base, so each is equally powerful for application development. Personally, I often go back-and-forth between the languages (for this column and customer projects). This week we'll take a closer look at the numerous language differences (mostly syntactical) between VB.NET and C#.
Make your case
In my opinion, the biggest difference (by far) between VB.NET and C# is the fact that C# is case-sensitive. This can be a problem for former Visual Basic or current VB.NETers to grasp as they move to C#. After all, the following is legal in C#:
stringfirstName;
string FirstName;
string Firstname;
These three variables are unique with no problems, but the same (or similar) VB.NET code will not compile since each variable is seen as having the same name:
Dim firstName As String
dim FirstName As String
Dim Firstname As string
Other languages like JavaScript and Java are case-sensitive, so this isn't an anomaly—rather developers may view VB.NET's lack of case-sensitive support as out-of-the-ordinary.
Delimiters
Another major difference in the languages is line termination and code blocks. VB.NET uses a carriage return at the end of each line, so lines of code are contained on their own line. On the other hand, C# code lines use the semi-colon at the end of each line of code. Each line of code does not need to be on its own line, but it must end with a semi-colon. If you forget a semi-colon, you'll encounter a code error.
A block of code is a collection of statements. Examples include a class, its methods, loops, and so forth. C# marks a code block with curly braces (the starting brace { and the ending brace }) while VB.NET ends code blocks with statements (such as End If, Next, and End Sub). The following C# code snippet declares a class with a constructor with the curly braces defining the groups:
public class TestClass {
public TestClass() {
// Constructor
}
~TestClass() {
// Destructor
}
public void DoSomething() { }
public intDoSomethingElse() {}
}
And, VB.NET uses end statements to delineate a code block:
Class TestClass
Public Sub New()
' Constructor
End Sub
Protected Overrides Sub Finalize()
REM Destructor
End Sub
Public Sub DoSomething()
End Sub
Public Function DoSomethingElse() As Integer
End Function
End Class
These code snippets demonstrate the approach to blocks of code—most notably, a class declared with two methods. Actually, the code demonstrates a few other points. Each class declares constructor and destructor methods as well as two methods. The first method (DoSomething) does not return a value, while the second method (DoSomethingElse) does. You'll notice that VB.NET uses Sub for methods that return no values and Function for its counterpart. C# uses void for no return value and specifies the return value otherwise. The code also uses comments.
The C# comments are single-line with the double backslashes (//); multi-line comments may be used as well—they are enclosed as a group between backslash and asterisk combinations (/* and */). VB.NET only supports single-line comments denoted with either the apostrophe (') or REM statement.
Data types
The two languages support the same data types with a few differences in syntax. The following table provides an overview.
|
VB.NET |
C# |
|
Boolean |
bool |
|
Byte |
byte, sbyte |
|
Char |
char |
|
Short, Integer, Long |
short, ushort, int, unit, long, ulong |
|
Single, Double |
float, double |
|
Decimal |
decimal |
|
Date |
DateTime |
|
Object |
object |
|
String |
string |
You may notice a big difference where C# supports unsigned values while VB.NET does not. Variable declaration presents differences as well. VB.NET uses the dim statement to declare (or dimension) a variable, and the as keyword is used to assign a type as the following lines demonstrate:
Dim i As Integer
Dim first As String
On the other hand, C# starts with the type followed by the name:
int I;
string first;
When working with variables and objects, you will often work with arrays where another syntax difference arises.
Brackets
One of the language differences that always seem to trip me up is the use of brackets by C# for accessing individual elements of an array or collection of values. On the flip side, VB.NET uses parentheses in the same situations. The following code declares and populates an array of integer values:
int[] test = {1,2,3,4,5};
for (int x=0; x < test.Length; x++) { Console.WriteLine(test[x]); };
Here's the equivalent VB.NET code:
Dim test() As Integer = {1, 2, 3, 4, 5}
For i As Integer = 0 To (test.Length - 1)
Console.WriteLine(test(i))
Next i
The code demonstrates the different syntax of the for loop as well.
Equality
The final discrepancy between the languages that I'd like to cover is the syntax for determining the equality of two values. This is a common misstep for developers new to C# (and JavaScript coders as well), as C# uses the double equals sign (==) to test for equality, while VB.NET uses the single equals sign (=). The following code samples demonstrate testing equality—beginning with C# that determines if an integer is equal to a value and displays text accordingly:
if (i == 2) { Console.WriteLine("They are equal"); }
else { Console.WriteLine("They are not equal"); }
Here's the VB.NET equivalent:
Dim i As Integer
If (i = 2) Then
Console.WriteLine("They are equal")
Else
Console.WriteLine("They are not equal")
End If
The frustrating aspect of this difference is it is not caught during compilation, so using the single equals sign in C# will assign the value instead of determining equality and thus logic problems that requires debugging.
Just the beginning
I've highlighted some of the common differences between C# and VB.NET. There are many more syntactical differences, but you'll discover those as you dig into the languages. Hopefully, the anomalies pointed out will prove fruitful if you find yourself transitioning to the other language.
Miss a column?
Check out the .NET Archive, and catch up on the most recent editions of Tony Patton's column.
Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.
Print/View all Posts Comments on this article
SponsoredWhite Papers, Webcasts, and Downloads
- Improving Decision Making Through Enterprise Information Management SAP
- Choosing the Best CRM for Your Organization Oracle
- Network Readiness For VoIP ShoreTel
- 5 Steps to Successful IT Consolidation Riverbed
- Economist Intelligence Unit whitepaper: "Enterprise Knowledge Workers: Understanding Risks and Opportunities" SAP
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET
