Professional Documents
Culture Documents
Advantages of Both Languages: Microsoft - Visualbasic
Advantages of Both Languages: Microsoft - Visualbasic
Advantages of Both Languages: Microsoft - Visualbasic
VB.NET C#
• Support for optional parameters - very • XML documentation generated from
handy for some COM interoperability. source code comments. (This is
• Support for late binding with Option Strict coming in VB.NET with Whidbey
off - type safety at compile time goes out of (the code name for the next version of
the window, but legacy libraries which don't Visual Studio and .NET), and there
have strongly typed interfaces become are tools which will do it with existing
easier to use. VB.NET code already.)
• Support for named indexers. • Operator overloading - again, coming
• Various legacy VB functions (provided in to VB.NET in Whidbey.
the Microsoft.VisualBasic namespace, • Language support for unsigned types
and can be used by other languages with a (you can use them from VB.NET, but
reference to the Microsoft.VisualBasic.dll). they aren't in the language itself).
Many of these can be harmful to Again, support for these is coming to
performance if used unwisely, however, and VB.NET in Whidbey.
many people believe they should be avoided • The using statement, which makes
for the most part. unmanaged resource disposal simple.
• The with construct: it's a matter of debate as • Explicit interface implementation,
to whether this is an advantage or not, but where an interface which is already
it's certainly a difference. implemented in a base class can be re-
• Simpler (in expression - perhaps more implemented separately in a derived
complicated in understanding) event class. Arguably this makes the class
handling, where a method can declare that it harder to understand, in the same way
handles an event, rather than the handler that member hiding normally does.
having to be set up in code.
• The ability to implement interfaces with • Unsafe code. This allows pointer
methods of different names. (Arguably this arithmetic etc, and can improve
makes it harder to find the implementation performance in some situations.
of an interface, however.) However, it is not to be used lightly,
• Catch ... When ... clauses, which allow as a lot of the normal safety of C# is
exceptions to be filtered based on runtime lost (as the name implies). Note that
expressions rather than just by type. unsafe code is still managed code, i.e.,
it is compiled to IL, JITted, and run
• The VB.NET parts of Visual Studio .NET within the CLR.
compiles your code in the background.
While this is considered as an advantage for
small projects, people creating very large
projects have found that the IDE slows
down considerably as the project gets larger.
Keyword Differences
Purpose VB.NET C#
Declare a variable Private, Public, Friend, declarators (keywords include user-
Protected, Static1, Shared, defined types and built-in types)
Dim
Declare a named Const const
constant
Create a new object New, CreateObject() new
Function/method does Sub void
not return a value
Overload a function or Overloads (No language keyword required for this
method (Visual Basic: purpose)
overload a procedure or
method)
Refer to the current Me this
object
Make a nonvirtual call MyClass n/a
to a virtual method of
the current object
Retrieve character from GetChar Function []
a string
Declare a compound Structure <members> End struct, class, interface
Structure
data type (Visual
Basic: Structure)
Initialize an object Sub New() Constructors, or system default type
(constructors) constructors
Terminate an object n/a n/a
directly
Method called by the Finalize destructor
system just before
garbage collection
reclaims an object7
Initialize a variable Collapse Collapse
where it is declared Dim x As Long = 5 // initialize to a value:
Collapse
Dim c As New _ int x = 123;
Car(FuelTypeEnum.Gas) // or use default
// constructor:
Operators Differences
Purpose VB.NET C#
Integer division \ /
Modulus (division Mod %
returning only the
remainder)
Exponentiation ^ n/a
Integer division \= /=
Assignment
Concatenate &= NEW +=
Modulus n/a %=
Bitwise-AND n/a &=
Bitwise-exclusive-OR n/a ^=
Bitwise-inclusive-OR n/a |=
Equal = ==
Not equal <> !=
Compare two object Is ==
reference variables
Compare object reference TypeOf x Is Class1 x is Class1
type
Concatenate strings & +
Shortcircuited Boolean AndAlso &&
AND
Shortcircuited Boolean OR OrElse ||
Scope resolution . . and base
Array element () [ ]
Type cast Cint, CDbl, ..., CType (type)
Postfix increment n/a ++
Postfix decrement n/a --
Indirection n/a * (unsafe mode only)
Address of AddressOf & (unsafe mode only; also see fixed)
Logical-NOT Not !
One's complement Not ~
Prefix increment n/a ++
Prefix decrement n/a --
Size of type n/a sizeof
Bitwise-AND And &
Bitwise-exclusive-OR Xor ^
Bitwise-inclusive-OR Or |
Logical-AND And &&
Logical-OR Or ||
Conditional If Function () ?:
Pointer to member n/a . (Unsafe mode only)
Programming Difference
Purpose VB.NET C#
Declaring Collapse Collapse
Variables Dim x As Integer int x;
Public x As Integer = 10 int x = 10;
Comments Collapse Collapse
' comment // comment
n += 1
End While '
' by enclosing
ABC((x))
Parameter Collapse Collapse
Passing by Public Sub ABC(ByRef y As Long) /* Note that there is no
Reference ' The parameter y is declared way to pass reference types
(objects) strictly by value.
'by referece: You can choose to either
pass the reference
' If ABC changes y, the changes are (essentially a pointer),
or a reference to the
' made to the value of x. reference (a pointer to a
pointer).*/
End Sub // Note also that unsafe C#
// The method:
ABC(ref i);
Structured Collapse Collapse
Exception Try // try-catch-finally
If x = 0 Then
Handling
Throw New Exception( _ try
"x equals zero") {
Else if (x == 0)
Throw New Exception( _ throw new System.Exception(
"x does not equal zero") "x equals zero");
End If else
Catch err As System.Exception throw new System.Exception(
MsgBox( _ "x does not equal zero");
"Error: " & Err.Description) }
Finally catch (System.Exception err)
MsgBox( _ {
"Executing finally block.") System.Console.WriteLine(
End Try err.Message);
}
finally
{
System.Console.WriteLine(
"executing finally block");
}