VB.NET
Program Structure
C#
Imports System
Namespace Hello
Class HelloWorld
Overloads Shared Sub
Main(ByVal args() As String)
Dim name As String = "VB.NET"
'See if an argument was
passed from the command line
If args.Length = 1 Then name = args(0)
Console.WriteLine("Hello, " & name
& "!")
End Sub
End Class
End Namespace
using System;
namespace Hello {
public class HelloWorld
{
public static void Main(string[]
args) {
string name = "C#";
// See if an argument was passed from the
command line
if (args.Length == 1)
name = args[0];
Console.WriteLine("Hello, " + name + "!");
}
}
}
VB.NET
Comments
C#
' Single line onlyREMSingle
line only
'''
XML
comments
// Single line///
XML comments on single
line
VB.NET
Data Types
C#
Value TypesBoolean
Byte, SByte
Char
Short, UShort, Integer, UInteger, Long, ULong
Single, Double
Decimal
Date
Reference TypesObject
String
InitializingDim correct As Boolean = True
Dim b As Byte = &H2A 'hex
Dim o As Byte = &O52 'octal
Dim person As Object = Nothing
Dim name As String = "Dwight"
Dim grade As Char = "B"c
Dim today As Date = #12/31/2007 12:15:00 PM#
Dim amount As Decimal = 35.99@
Dim gpa As Single = 2.9!
Dim pi As Double = 3.14159265
Dim lTotal As Long = 123456L
Dim sTotal As Short = 123S
Dim usTotal As UShort = 123US
Dim uiTotal As UInteger = 123UI
Dim ulTotal As ULong = 123UL
Type InformationDim x
As
Integer
Console.WriteLine(x.GetType()) ' Prints
System.Int32
Console.WriteLine(GetType(Integer)) ' Prints System.Int32
Console.WriteLine(TypeName(x)) ' Prints Integer
Type ConversionDim 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)
Value Typesbool
byte, sbyte
char
short, ushort, int, uint, long, ulong
float, double
decimal
DateTime (not a built-in
C# type)
Reference Typesobject
string
Initializingbool correct = true;
byte b = 0x2A; //
hex
object person = null;
string name = "Dwight";
char grade = 'B';
DateTime today = DateTime.Parse("12/31/2007 12:15:00");
decimal amount = 35.99m;
float gpa = 2.9f;
double pi = 3.14159265;
long lTotal = 123456L;
short sTotal = 123;
ushort usTotal = 123;
uint uiTotal = 123;
ulong ulTotal = 123;
Type Informationint x;
Console.WriteLine(x.GetType()); // Prints
System.Int32
Console.WriteLine(typeof(int)); // Prints
System.Int32
Console.WriteLine(x.GetType().Name); // prints Int32
Type Conversion float d = 3.5f;
int i = (int)d; // set
to 3 (truncates
decimal)
VB.NET
Constants
C#
ConstMAX_STUDENTS
As
Integer = 25
' Can set to a const or var; may
be initialized in a constructorReadOnly MIN_DIAMETER As Single = 4.93
const int MAX_STUDENTS = 25;
// Can set to a const or var; may be initialized
in a constructor readonly float MIN_DIAMETER = 4.93f;
VB.NET
Enumerations
C#
Enum Action
Start [Stop] '
Stop is a reserved word
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 _
Console.WriteLine(a.ToString
& " is " &
a) ' Prints "Stop is 1"
Console.WriteLine(Status.Pass) ' Prints 70
Console.WriteLine(Status.Pass.ToString()) ' Prints
Pass
enum Action {Start, Stop, Rewind, Forward};
enum Status
{Flunk = 50, Pass = 70, Excel = 90};
Action a = Action.Stop;
if (a != Action.Start)
Console.WriteLine(a + " is " + (int)
a); //
Prints "Stop is 1"
Console.WriteLine((int)
Status.Pass); // Prints 70
Console.WriteLine(Status.Pass); //
Prints Pass
VB.NET
Operators
C#
Comparison= <= >= <>
Arithmetic+ - * /
Mod
\ (integer
division)
^ (raise to a
power)
Assignment= += -= *= /= \=
^= <<=
>>= &=
BitwiseAnd Or Xor Not <<
>>
LogicalAndAlso OrElse And
Or Xor Not
Note: AndAlso and OrElse perform short-circuit logical
evaluations
String Concatenation&
Comparison== <= >= !=
Arithmetic+ - * /
% (mod)
/ (integer division if
both operands are ints)
Math.Pow(x, y)
Assignment= += -=
*= /= %= &= |= ^= <<= >>= ++ --
Bitwise& | ^
~ << >>
Logical&& ||
& |
^ !
Note: &&
and || perform short-circuit
logical evaluations
String Concatenation&