« Buffered Console.Out | Main | Lessons learned from FxCop »

VB vs. C# Comparison

The purpose of this table is to save time for people who spend most of their time with one language and need a quick reminder on analagous syntax in the other language.

Based on an original table by Dan Appleman.

VB.NET

C#

Comments

NotInheritable

sealed

Specifies that a class cannot be used as a base class, i.e. that it cannot be inherited.

NotOverridable

sealed

Specifies that a method cannot be overridden.

MustInherit

abstract

Specifies that a class can only be inherited (an instance of the class cannot be created).

MustOverride

abstract

Specified that a method must be implemented in a derived class.

Overridable

virtual

Specifies that a member can be overridden.

Shared

static

Specifies that a member is shared by all instances of a class. An instance of the class is not required to call the member.

Static

no equivalent

Specifies that a local variable's value is preserved between calls.

Public

public

Class/member is visible outside of project or assembly.

Friend

internal

Class/member is invisible outside of the assembly.

Private

private

Class/member is visible only within the project.

Overloads

not required

Specifies that a member is overloading another member.

Overrides

override

Specifies that a member is overriding another member.

Implements I1

class C1:I1

Specifies that the class (C1) implements the interface I1.

Inherits C2

class C1:C2

Specifies that the class (C1) inherits class C2.

Implements I1

Inherits C2

class C1:C2,I1

Specifies that the class (C1) implements the interface I1 and inherits class C2.

Shadows

new

 

Finalize

~C1 (destructor)

Method called by system just before garbage collection reclaims object. C1 is classname.

New

C1

Constructor method, called when object is created. C1 is classname.

Dim x as Int32

Int32 x

Declares variable x as type System.Int32.

Dim t1 As New SomeType()
Try
t1.SomeMethod()
Finally
If (t1 Is Nothing) Then
If TypeOf t1 Is IDisposable Then
CType(t1, IDisposable).Dispose()
End If
End If
End Try

using(SomeType t1 = new SomeType())
{
t1.SomeMethod;
}

 

Imports

using

Allows methods to be called without fully qualifying with Namespace name.

<> 

[]

Specifies parameters.

Function x as Int32

Sub y

Int32 x

void y

Specifies return values (Int32 and none).

_

;

Line continuation character in VB, end of line character in C#.

AndAlso

OrElse

&&

||

Short circuit versions of And and Or.

not supported

<< >>

Shift operators.

X+=1 (x=x+1)

X-=1 (x=x-1)

also *=, /=, ^= etc..

x++

x--

Short cut incrementers.

Dim x(4) as Int32

= 5 items (0 to 4)

Int32[] x = new Int32[4];

= 4 items (0 to 3)

Differences in array declarations and the number of elements created.

Dim x as Int32

Int32 x = 0

x initialized as zero automatically in VB.

ReDim Preserve

no equivalent

Redimensions an array.

Optional

need to overload sub instead

Specifies and optional parameter.

Select Case x
Case True
Case Else
End Select

switch (I) {
     case 1: break;
     default: break;
}

 

ByVal

ByRef

not required

ref

Passing parameters by value and by reference.

Me

this

Refer to the current object.

MyBase

base

Refer to the base class.

MyClass

no eqivalent

Make a non-virtual call to a virtual method of the current object.

Const

const

readonly

Declare a constant.

Enum

enum

Declare an enumerator.

Structure

struct

Declare a structure.

no equivalent

volatile

Declare that an object can be modified asynchronously.

obj = Nothing

obj == null

Test for an object variable that does not point to anything.

Option Explicit

on by default and not changeable

Specify that all variables must be declared.

IsDBNull

no equivalent

Test for a database Null.

Default

must use indexer

Specify default method of a class.

WithEvents

no equivalent

Declare a variable whose events you wish to handle.

Handles

no equivalent

Specify that the method is an event sink.

Try
Catch
Finally
End Try

try{

} catch {

} finally {

}

Structured exception handling

If Number < 10 Then
     Digits = 1
ElseIf Number < 100 Then
     Digits = 2
Else
     Digits = 3
End If

if (x > 10) {
     if (y > 20) x = 3;
} else x = 5;

Conditions.

For I = 1 To 10
 ' Statements
Next I

for (int i = 1; i <= 5; i++)
     Console.WriteLine(i);

Iterations.

REM

'

/* ... */

//

Comment lines

not supported

///

XML comment lines

Dim y As Char = GetChar("Hello", 1)

char y = "hello"[0];

Retrieve a character from a String.

AddressOf

delegate

Use the address of a method.

With ... End With

no equivalent

Evaluate an object one and use many times.

Dim a() as Long = {1,2,3}

int[] x = new int[4] {1,2,3,4};

Initialize an array.

Event

RaiseEvent

event delegate eventName;

eventName(<delegate’s args>)

Declare and raise an event.

SyncLock

lock

Threading primitives.

Defaults to Private Scope

     Variable in class

Defaults to Public Scope

     Variable in Structure
     Structure
     Class
     Method in Class
     Method in Structure

Defaults to private

     Variable in class
     Variable in struct
     Method in class
     Method in struct

Defaults to public

     struct
     class

 Access modifier defaults.

no equivalent

void f(out string arg) {

     arg = "Hello World";
}

An output parameter is a parameter that is passed from a called method to the method that called it, that is, the reverse direction. Output parameters are useful if you want a method to return more than a single value.

 

@"C:\myHelpFile.htm"

 Raw character string constants.

AND OR NOT

& | ^

Bitwise binary operators.

CType(bar, typeFoo)

(typeFoo)bar

Type conversion, type cast.

(int)aIntPtr

aIntPtr.ToInt32()

Converting a System.IntPtr to integer

Post a comment