Escolar Documentos
Profissional Documentos
Cultura Documentos
Some people like VB.NET's natural language, case-insensitive approach, others like C#'s
terse syntax. But both have access to the same framework libraries. We will discuss about
the differences in the following topics:
• The VB.NET parts of Visual Studio • Unsafe code. This allows pointer
.NET compiles your code in the arithmetic etc, and can improve
background. While this is considered as performance in some situations.
an advantage for small projects, people However, it is not to be used
creating very large projects have found lightly, as a lot of the normal
that the IDE slows down considerably as safety of C# is lost (as the name
the project gets larger. implies). Note that unsafe code is
still managed code, i.e., it is
compiled to IL, JITted, and run
within the CLR.
Keyword Differences
Purpose VB.NET C#
Declare a variable Private, Public, Friend, declarators (keywords include user-
Protected, Static1, defined types and built-in types)
Shared, Dim
Declare a named Const const
constant
Create a new object New, CreateObject() new
Function/method Sub void
does not return a
value
Overload a function Overloads (No language keyword required for
or method (Visual this purpose)
Basic: overload a
procedure or method)
Refer to the current Me this
object
Make a nonvirtual MyClass n/a
call to a virtual
method of the current
object
Retrieve character GetChar Function []
from 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 Dim x As Long = 5 // initialize to a value:
Dim c As New _ int x = 123;
where it is declared
Car(FuelTypeEnum.Gas // or use default
) // constructor:
int x = new int();
Take the address of a AddressOf (For class delegate
function members, this operator
returns a reference to a
function in the form of a
delegate instance)
Declare that an object n/a volatile
can be modified
asynchronously
Force explicit Option Explicit n/a. (All variables must be declared
declaration of prior to use)
variables
Test for an object obj = Nothing obj == null
variable that does not
refer to an object
Value of an object Nothing null
variable that does not
refer to an object
Test for a database IsDbNull n/a
null expression
Test whether a n/a n/a
Variant variable has
been initialized
Define a default Default by using indexers
property
Refer to a base class MyBase base
Declare an interface Interface interface
Specify an interface Implements (statement) class C1 : I1
to be implemented
Declare a class Class <implementation> class
Specify that a class MustInherit abstract
can only be inherited.
An instance of the
class cannot be
created.
Specify that a class NotInheritable sealed
cannot be inherited
Declare an Enum <members> End Enum enum
enumerated type
Declare a class Const const (Applied to a field declaration)
constant
Derive a class from a Inherits C2 class C1 : C2
base class
Override a method Overrides override
Declare a method that MustOverride abstract
must be implemented
in a deriving class
Declare a method that NotOverridable (Methods sealed
can't be overridden are not overridable by
default.)
Declare a virtual Overridable virtual
method, property
(Visual Basic), or
property accessor
(C#, C++)
Hide a base class Shadowing n/a
member in a derived
class
Declare a typesafe Delegate delegate
reference to a class
method
Specify that a WithEvents (Write code - no specific keyword)
variable can contain
an object whose
events you wish to
handle
Specify the events for Handles (Event procedures n/a
which an event can still be associated with a
procedure will be WithEvents variable by
called naming pattern.)
Evaluate an object With objExpr n/a
<.member>
expression once, in
<.member>
order to access End With
multiple members
Structured exception Try <attempt> try, catch, finally, throw
Catch
handling
<handle errors>
Finally
<always execute>
End Try
Decision structure Select Case ..., Case, switch, case, default, goto, break
(selection) Case Else, End Select
Decision structure If ... Then, ElseIf ... if, else
(if ... then) Then, Else, End If
Loop structure While, Do [While, Until] do, while, continue
(conditional) ..., Loop [While, Until]
Loop structure For ..., [Exit For], for, foreach
(iteration) Next
For Each ..., [Exit
For,] Next
Declare an array Dim a() As Long int[] x = new int[5];
Initialize an array Dim a() As Long = {3, int[] x = new int[5] {
4, 5} 1, 2, 3, 4, 5};
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 TypeOf x Is Class1 x is Class1
reference type
Concatenate strings & +
Shortcircuited Boolean AndAlso &&
AND
Shortcircuited Boolean OrElse ||
OR
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 Dim x As Integer int x;
Public x As Integer = 10 int x = 10;
Variables
Comments ' comment // comment
x = 1 ' comment /* multiline
Rem comment comment */
Assignment nVal = 7 nVal = 7;
Statements
Conditional If nCnt <= nMax Then if (nCnt <= nMax)
' Same as nTotal = {
Statements ' nTotal + nCnt. nTotal += nCnt;
nTotal += nCnt nCnt++;
' Same as nCnt = nCnt + 1. }
nCnt += 1 else
Else {
nTotal += nCnt nTotal +=nCnt;
nCnt -= 1 nCnt--;
End If }
Selection Select Case n switch(n)
Case 0 {
Statements MsgBox ("Zero") case 0:
' Visual Basic .NET exits Console.WriteLine("Zero
' the Select at ");
' the end of a Case. break;
Case 1 case 1:
MsgBox ("One") Console.WriteLine("One"
Case 2 );
MsgBox ("Two") break;
Case Else case 2:
MsgBox ("Default") Console.WriteLine("Two"
End Select );
break;
default:
Console.WriteLine("?");
break;
}