VB.NET and C# Comparison
This is a quick reference guide to highlight some key syntactical
differences between VB.NET (version 2) and C#. Hope you find this useful!
Thank you to Tom Shelton, Fergus Cooney, Steven Swafford and others for your
contributions.
Also see Java and C# Comparison.
|
|||||||||||
Imports System
Namespace Hello 'See if an |
using System;
namespace Hello { // See if an Console.WriteLine("Hello, " + name |
||||||||||
|
|||||||||||
' Single line only Rem Single line only |
// Single line |
||||||||||
|
|||||||||||
Value Types Reference Types Dim x As Integer ' Type conversion |
Value Types Reference Types int x;
|
||||||||||
|
|||||||||||
Const MAX_STUDENTS As Integer = 25 ' Can set to a const or var; may be initialized in a |
const int MAX_STUDENTS = 25;
// Can set to a const or var; may be initialized in a |
||||||||||
|
|||||||||||
Enum Action Start [Stop] ' Stop is a reserved word Rewind Forward End Enum Enum Status Console.WriteLine(Status.Pass) ' |
enum Action {Start, Stop, Rewind, Forward}; enum Status {Flunk = 50, Pass = 70, Excel = 90}; Action a = Action.Stop; Console.WriteLine((int) Status.Pass); // |
||||||||||
|
|||||||||||
Comparison Arithmetic Assignment Bitwise Logical Note: AndAlso and OrElse perform short-circuit logical String Concatenation |
Comparison Arithmetic Assignment Bitwise Logical Note: && and || perform short-circuit logical String Concatenation |
||||||||||
|
|||||||||||
greeting = IIf(age < 20, "What's up?", "Hello") ' One line doesn't require "End If" ' Use : to put two commands on same line ' Preferred ' To break up any long single line use _ 'If x > 5 Then Select Case color ' Must be a |
greeting = age < 20 ? "What's up?" : "Hello";
if (age < 20)
// Multiple statements must be enclosed in {} No need for _ or : since ; is used to terminate each
// Every case must end with break or goto case |
||||||||||
|
|||||||||||
' Array or collection looping
' Breaking out of loops
' Continue to next iteration |
Pre-test Loops:
// no "until" keyword for (c = 2; c <= 10; c += 2) Post-test Loop: do // Array or collection looping
// Breaking out of loops
// Continue to next iteration |
||||||||||
|
|||||||||||
Dim nums() As Integer = {1, 2, 3} ' 4 is the index of the last element, so it holds 5 ' Resize the array, keeping the existing values (Preserve is Dim twoD(rows-1, cols-1) As Single Dim jagged()() As Integer = { _ |
int[] nums = {1, 2, 3}; // 5 is the size of the array // C# can't dynamically resize an array. Just copy into float[,] twoD = new float[rows, cols]; int[][] jagged = new int[3][] { |
||||||||||
|
|||||||||||
' Pass by value (in, default), reference (in/out), Dim a = 1, b = 1, c As Integer ' c set to ' Accept variable number of arguments Dim total As Integer = Sum(4, 3, 2, 1) ' returns ' Optional parameters must be listed last and must SayHello("Strangelove", "Dr.") |
// Pass by value (in, default), reference (in/out), and reference (out) void TestFunc(int x, ref int y, out int z) { x++; y++; z = 5; } int a = 1, b = 1, c; // c doesn't need initializing // Accept variable number of arguments int total = Sum(4, 3, 2, 1); // returns 10 /* C# doesn't support optional void SayHello(string name) { |
||||||||||
|
|||||||||||
Special character constants
' String concatenation (use & or +) ' Chars ' No string literal operator
' String comparison Console.WriteLine(mascot.Substring(2, 3)) ' Prints "son"
' String matching Imports System.Text.RegularExpressions ' More powerful than Like ' My birthday: Oct 12, 1973 ' Mutable string |
Escape sequences // String concatenation // Chars // String literal // String comparison Console.WriteLine(mascot.Substring(2, 3));
// String matching using System.Text.RegularExpressions; // My birthday: Oct 12, 1973 // Mutable string |
||||||||||
|
|||||||||||
' Throw an exception ' Catch an exception ' Deprecated unstructured error handling |
// Throw an exception // Catch an exception |
||||||||||
|
|||||||||||
Namespace Harding.Compsci.Graphics ' or Namespace Harding Imports Harding.Compsci.Graphics |
namespace Harding.Compsci.Graphics { // or namespace Harding { using Harding.Compsci.Graphics; |
||||||||||
|
|||||||||||
Accessibility keywords ' Inheritance ' Interface definition // Extending an interface // Interface implementation |
Accessibility keywords // Inheritance
// Extending an interface
|
||||||||||
|
|||||||||||
Class SuperHero Private _powerLevel As Integer Public Sub New() Public Sub New(ByVal powerLevel As Integer) Protected Overrides Sub Finalize() |
class SuperHero { public SuperHero() { public SuperHero(int powerLevel) { ~SuperHero() { |
||||||||||
|
|||||||||||
Dim hero As SuperHero = New SuperHero
With hero
hero.Defend("Laura Jones") Dim hero2 As SuperHero = hero ' Both reference the same hero = Nothing ' Free the object If hero Is Nothing Then _ Dim obj As Object = New SuperHero
' Mark object for quick disposal |
SuperHero hero = new SuperHero();
// No "With" construct hero.Defend("Laura Jones"); SuperHero hero2 = hero; // Both reference the same hero = null ; // Free the object if (hero == null) Object obj = new SuperHero(); // Mark object for quick disposal |
||||||||||
|
|||||||||||
Structure StudentRecord Public Sub New(ByVal name As String, ByVal gpa As Single) Dim stu As StudentRecord = New StudentRecord("Bob", 3.5) stu2.name = "Sue" |
struct StudentRecord { public string name; public float gpa; public StudentRecord(string name, float gpa) { StudentRecord stu = new StudentRecord("Bob", 3.5f); stu2.name = "Sue"; |
||||||||||
|
|||||||||||
Private _size As Integer Public Property Size() As Integer foo.Size += 1 |
private int _size;
public int Size { foo.Size++; |
||||||||||
|
|||||||||||
Delegate Sub MsgArrivedEventHandler(ByVal message As String) Event MsgArrivedEvent As MsgArrivedEventHandler ' or to define an event which declares a delegate implicitly AddHandler MsgArrivedEvent, AddressOf My_MsgArrivedCallback Imports System.Windows.Forms Dim WithEvents MyButton As Button Private Sub MyButton_Click(ByVal sender As System.Object, _ |
delegate void MsgArrivedEventHandler(string message); event MsgArrivedEventHandler MsgArrivedEvent; // Delegates must be used with events in C#
MsgArrivedEvent += new using System.Windows.Forms; Button MyButton = new Button(); private void MyButton_Click(object sender, System.EventArgs e) { |
||||||||||
|
|||||||||||
Console.Write("What's your name? ") Dim c As Integer |
Console.Write("What's your name? ");
int c = Console.Read(); // Read single char |
||||||||||
|
|||||||||||
Imports System.IO ' Write out to text file ' Read all lines from text file ' Write out to binary file ' Read from binary file |
using System.IO; // Write out to text file // Read all lines from text file // Write out to binary file // Read from binary file |