Difference - VB - Net & C#
Difference - VB - Net & C#
Visual Basic .NET and Visual C# .NET differ in terms of case sensitivity, variable declaration
and assignment, data types, statement termination, statement blocks, use of parentheses
versus brackets, operators, conditional statements, error handling, overflow checking,
parameter passing, late binding, ways of handling unmanaged code, and keywords.
Coalesce
INTRODUCTION
Because of the past differences between Microsoft® Visual Basic™, Microsoft®
Visual C™, and Microsoft® Visual C++™, many developers have the impression
that Microsoft® Visual C# .NET™ is a more powerful language than Microsoft®
Visual Basic .NET™. Some developers assume that many things that are possible
in Visual C# .NET are impossible in Visual Basic .NET, just as many things that
are possible in Microsoft® Visual C™ 6.0 and earlier or Microsoft® Visual C++™
6.0 and earlier are impossible in Microsoft® Visual Basic™ 6.0 and earlier. This
assumption is incorrect. Although differences exist between Visual Basic .NET and
Visual C# .NET, they are both first-class programming languages that are based
on the Microsoft® .NET Framework, and they are equally powerful. Visual Basic
.NET is a true object-oriented programming language that includes new and
improved features such as inheritance, polymorphism, interfaces, and
overloading. Both Visual Basic .NET and Visual C# .NET use the common
language runtime in the .NET Framework, and almost no performance issues now
exist between them. Visual Basic .NET may be oriented more toward ease of use
by providing features such as late binding, and Visual C# .NET may have a few
more “power” features, such as handling unmanaged code, but the differences
are very small compared to what they were in the past.
This document discusses differences between Visual Basic .NET and Visual C#
.NET. However, the key point to keep in mind is that .NET is intended to be
language-independent. The choice between Visual Basic .NET and Visual C# .NET
is typically based on your personal preference and past experience; for example,
it is easier for Visual Basic 6.0 developers to use Visual Basic .NET, and for Visual
C++ and Java programmers to use Visual C# .NET. The existing experience of a
programmer far outweighs the small differences between the two languages.
1
Coalesce
• Operators
• Conditional statements
• Error handling
• Overflow checking
• Parameter passing
• Late binding
• Handling unmanaged code
• Keywords
Case Sensitivity
Identifier names in Visual Basic .NET are not case-sensitive, but identifier names
in Visual C# .NET are. This primarily presents a problem when you write code,
and is not an issue in debugging a program that already compiles.
2
Coalesce
Data Types
Simple data types have different names in Visual Basic .NET and Visual C# .NET. For
example, Integer in Visual Basic .NET is int in Visual C# .NET. However,
System.Int32, the .NET Framework base type for which Integer and int are
aliases, can be used in both languages. Visual C# .NET also supports the signed
byte, unsigned short, unsigned int, and unsigned long data types, which are
not available in Visual Basic .NET.
The following table lists the different data type names in each language and the base
types for which they are aliases.
Statement Termination
Statements in Visual Basic .NET are terminated by the end of the line. You can use
the colon (:) to put multiple statements in a line, and you can use the line
continuation (_) character to make a statement span several lines.
Statements in Visual C# .NET are terminated by the semicolon (;). You can use
multiple statements per line, and statements can span multiple lines.
3
Coalesce
Statement Blocks
Visual Basic .NET does not use arbitrary statement blocks. Instead, certain
keywords that have a specialized terminating statement are used instead of the
statement blocks.
In Visual C# .NET, braces ({}) are used to delimit a statement block; otherwise, a
single statement is assumed.
or
if (a == 5)
DoSomething();
DoSomethingAgain(); //This is not
part of
//the if statement.
Use of () vs. [ ]
Visual Basic .NET uses parentheses () to delimit array elements, function arguments,
and property indexes.
Visual C# .NET uses parentheses () to delimit function arguments, and brackets ([])
to delimit array elements and property indexes.
4
Coalesce
Operators
The operators that are used in Visual Basic .NET and Visual C# .NET are quite
different. The following table lists the main operators. This information can also be
found in the Microsoft® Visual Studio .NET™ documentation.
Addition + +
Subtraction - -
Multiplicative
Multiplication * *
Division / /
Exponentiation ^ n/a
Assignment
Assignment = =
+= -= *= /* += -= *= /*
Integer division \= /= (depending on the operands)
Concatenate &= +=
Modulus n/a %=
XOR n/a ^=
OR n/a |=
Equal = ==
5
Coalesce
Shift
Scope resolution
Postfix
Member selection . .
Unary
Bitwise
Bitwise OR Or |
6
Coalesce
Logical OR Or ||
Conditional
Conditional IIf ?:
Pointer to member
Conditional Statements
The following table lists the differences in the conditional statements that Visual
Basic .NET and Visual C# .NET use.
Error Handling
Unstructured error handling is for backward compatibility. Visual Basic .NET supports
both structured and unstructured error handling, but Visual C# .NET supports only
structured error handling.
7
Coalesce
Overflow Checking
Visual Basic .NET has a project level setting to check for overflow. However, the
checking can only be turned on and off at the project level, instead of at the level of
an expression or a block of code. To turn overflow checking on and off, follow these
steps:
Parameter Passing
Visual Basic .NET uses ByVal for passing parameters by value, and uses ByRef for
passing parameters by reference. Visual Basic .NET can also force parameters to be
passed by value, regardless of how they are declared, by enclosing the parameters in
extra parentheses. Visual Basic .NET also supports optional parameters, which are
not available in Visual C# .NET.
Visual C# .NET does not have a way to pass reference types (objects) strictly by
value. You can either pass the reference (basically a pointer) or a reference to the
reference (a pointer to a pointer). Unmanaged Visual C# .NET methods can take
pointers just like Visual C++ methods. To pass a parameter by reference, Visual C#
.NET uses the ref keyword. To use a ref parameter, the argument must explicitly be
passed to the method as a ref argument. The value of a ref argument is passed to
the ref parameter.
8
Coalesce
ABC(x) ABC(i);
ABC((x))
Late Binding
Both Visual Basic .NET and Visual C# .NET can implement implicit late binding
through reflection. However, implementing late binding in Visual Basic .NET is much
easier than in Visual C# .NET.
In Visual Basic .NET, as in Visual Basic 6.0, the Visual Basic compiler calls a helper
method behind the scenes that uses reflection to obtain the object type. The
arguments that are passed to the helper method cause the appropriate method to be
invoked at run time. These arguments are the object on which to invoke the method,
the name of the invoked method that is a string, and the arguments that are passed
to the invoked method that is an array of objects. Additionally, you can implement
late binding explicitly in code through reflection.
Imports System
Module Hello
Sub Main()
' Set up variable.
Dim helloObj As Object
' Create the object.
helloObj = new HelloWorld()
' Invoke the print method as if it was early bound
' even though it is really late bound.
helloObj.PrintHello("Visual Basic Late Bound")
End Sub
End Module
In Visual C# .NET, implementing late binding is more difficult than in Visual Basic
.NET. Instead of having the compiler implement late binding, you must explicitly
implement late binding in code by using reflection.
9
Coalesce
Keywords
The following table lists the keywords that Visual Basic .NET and Visual C# .NET use
in several categories. This information can also be found in the Visual Studio .NET
online documentation.
10
Coalesce
11
Coalesce
12
Coalesce
Miscellaneous Lifetime
Other
13
Coalesce
14