VB.NET and C# ComparisonThis is a quick reference guide to highlight some key syntactical
differences between VB.NET and C#. Hope you find this useful!
Thank you to Tom Shelton, Fergus Cooney, Steven Swafford, Gjuro Kladaric, and others for your
contributions.
Also see Java and C# Comparison.

Dim c as New Circle
isShape = TypeOf c Is Shape
' True if c is a Shape

isSame = o1 Is o2 // True if o1 and o2 reference same object

Type Conversion / Casting
Dim d As Single = 3.5
Dim i As Integer = CType(d, Integer)
' set to 4 (Banker's rounding)
i = CInt(d) ' same result as CType
i = Int(d) ' set to 3 (Int
function truncates the decimal)

Dim obj As Object = New SuperHero
If TypeOf obj Is SuperHero Then _
Console.WriteLine("Is a SuperHero object.")

' Mark object for quick disposalUsing reader As StreamReader = File.OpenText("test.txt")
Dim line As String = reader.ReadLine()
While Not line Is Nothing
Console.WriteLine(line)
line = reader.ReadLine()
End WhileEnd Using

' Subroutine can display any type of List
Sub DisplayList(Of T)(ByVal list As List(Of T))
For Each item As T In list
Console.WriteLine(item)
Next
End Sub

' Class works on any data type
Class SillyList(Of T)
Private list(10) As T
Private rand As New Random

Public Sub Add(ByVal item As T)
list(rand.Next(10)) = item
End Sub

Public Function GetItem() As T
Return list(rand.Next(10))
End Function
End Class

' Limit T to only types that implement IComparable
Function Maximum(Of T As IComparable)(ByVal ParamArray items As T()) As T
Dim max As T = items(0)
For Each item As T In items
If item.CompareTo(max) > 0 Then max = item
Next
Return max
End Function

' Display all
For Each stu In students
Console.WriteLine(stu.Key & " = " & stu.Value)
Next

' Method iterator for custom iteration over a collectionIterator Function OddNumbers(ByVal lastNum As Integer) As System.Collections.IEnumerable
For num = 1 To lastNum
If num Mod 2 = 1 ThenYield num
End If
Next
End Function

' Read all lines from text file
Dim reader As StreamReader = File.OpenText("c:\myfile.txt")
Dim line As String = reader.ReadLine()
While Not line Is Nothing
Console.WriteLine(line)
line = reader.ReadLine()
End While
reader.Close()