C# and VB.NET Comparison Cheat Sheet
page 1 of 1
Published: 21 Feb 2005
Abstract
A quick cheat sheet that highlights some key syntactical differences between C# and VB.NET. While this is not all inclusive this cheat sheet proves to be a handy reference.
by Steven Swafford
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 140216/ 2275

C# and VB.NET Comparison Cheat Sheet

This document is an authorized derivative of Frank McCown's "VB.NET and C# Comparison" (C) 2005 at http://www.harding.edu/USER/fmccown/WWW/vbnet_csharp_comparison.html

This work is licensed under a Create Common License

[ Download Word Version or PDF Version ]

Comments

VB.NET

'Single line only

Rem Single line only

C#

// Single line

/* Multiple

line */

/// XML comments on single line

/** XML comments on multiple lines */

Program Structure

VB.NET

Imports System

Namespace MyNameSpace

  Class HelloWorld

    'Entry point which delegates to C-style main Private Function

    Public Overloads Shared Sub Main()

      Main(System.Environment.GetCommandLineArgs())

    End Sub

 

  Overloads Shared Sub Main(args() As String)

    System.Console.WriteLine("Hello World")

  End Sub 'Main

  End Class 'HelloWorld End Namespace 'MyNameSpace

C#

using System

Namespace MyNameSpace

{

  class HelloWorld

  {     

    static void Main(string[] args)

    {

      System.Console.WriteLine("Hello World")

    }

  }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Data Types

VB.NET

'Value Types

Boolean

Byte

Char (example: "A")

Short, Integer, Long

Single, Double

Decimal

Date

 

 

'Reference Types

Object

String

 

 

 

Dim x As Integer

System.Console.WriteLine(x.GetType())

System.Console.WriteLine(TypeName(x))

 

 

'Type conversion

Dim d As Single = 3.5

Dim i As Integer = CType (d, Integer)

i = CInt (d)

i = Int(d)

C#

//Value Types

bool

byte, sbyte

char (example: 'A')

short, ushort, int, uint, long, ulong

float, double

decimal

DateTime

 

 

//Reference Types

object

string

 

 

 

int x;

Console.WriteLine(x.GetType())

Console.WriteLine(typeof(int))

 

 

//Type conversion

float d = 3.5;

int i = (int) d

Constants

VB.NET

Const MAX_AUTHORS As Integer = 25

ReadOnly MIN_RANK As Single = 5.00

C#

const int MAX_AUTHORS = 25;

readonly float MIN_RANKING = 5.00;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Enumerations

VB.NET

Enum Action

  Start

  'Stop is a reserved word

[Stop]

  Rewind

  Forward

End Enum

 

Enum Status

   Flunk = 50

   Pass = 70

   Excel = 90

End Enum

 

Dim a As Action = Action.Stop

If a <> Action.Start Then _

'Prints "Stop is 1"

   System.Console.WriteLine(a.ToString & " is " & a)

 

'Prints 70

System.Console.WriteLine(Status.Pass)

'Prints Pass

System.Console.WriteLine(Status.Pass.ToString())

 

 

Enum Weekdays

   Saturday

   Sunday

   Monday

   Tuesday

   Wednesday

   Thursday

   Friday

End Enum 'Weekdays

C#

enum Action {Start, Stop, Rewind, Forward};

enum Status {Flunk = 50, Pass = 70, Excel = 90};

 

 

 

 

 

 

 

 

 

 

 

Action a = Action.Stop;

if (a != Action.Start)

//Prints "Stop is 1"

  System.Console.WriteLine(a + " is " + (int) a);

 

// Prints 70

System.Console.WriteLine((int) Status.Pass);

// Prints Pass

System.Console.WriteLine(Status.Pass);

 

 

 

enum Weekdays

{

  Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Operators

VB.NET

'Comparison

=  <  >  <=  >=  <>

 

 

'Arithmetic

+  -  *  /

Mod

\  (integer division)

^  (raise to a power)

 

 

'Assignment

=  +=  -=  *=  /=  \=  ^=  <<=  >>=  &=

 

 

'Bitwise

And  AndAlso  Or  OrElse  Not  <<  >>

 

 

'Logical

And  AndAlso  Or  OrElse  Not

 

 

'String Concatenation

&

C#

//Comparison

==  <