Professional Documents
Culture Documents
NET & C# QUICK REFERENCE CARD
Comments
VB.NET C#
'Single line only // Single line
Rem Single line only /* Multiple
line */
/// XML comments on single line
/** XML comments on multiple lines */
Program Structure
VB.NET C#
Imports System using System
Namespace MyNameSpace Namespace MyNameSpace
{
Class HelloWorld class HelloWorld
'Entry point which delegates to C-style main {
Private Function static void Main(string[] args)
{
Public Overloads Shared Sub Main() System.Console.WriteLine("Hello
Main(System.Environment.GetCommandLineArgs()) World")
End Sub }
}
Overloads Shared Sub Main(args() As String) }
System.Console.WriteLine("Hello World")
Data Types
VB.NET C#
'Value Types //Value Types
Boolean bool
Byte byte, sbyte
Char (example: "A") char (example: 'A')
Short, Integer, Long short, ushort, int, uint, long, ulong
Single, Double float, double
Decimal decimal
Date DateTime
1
Constants
VB.NET C#
Const MAX_AUTHORS As Integer = 25 const int MAX_AUTHORS = 25;
ReadOnly MIN_RANK As Single = 5.00 readonly float MIN_RANKING = 5.00;
Enumerations
VB.NET C#
Enum Action enum Action {Start, Stop, Rewind, Forward};
Start enum Status {Flunk = 50, Pass = 70, Excel =
'Stop is a reserved word 90};
[Stop]
Rewind
Forward
End Enum
Enum Status
Flunk = 50
Pass = 70
Excel = 90
End Enum
'Prints 70 // Prints 70
System.Console.WriteLine(Status.Pass) System.Console.WriteLine((int) Status.Pass);
'Prints Pass // Prints Pass
System.Console.WriteLine(Status.Pass.ToString()) System.Console.WriteLine(Status.Pass);
Enum Weekdays
Saturday enum Weekdays
Sunday {
Monday Saturday, Sunday, Monday, Tuesday,
Tuesday Wednesday, Thursday, Friday
Wednesday }
Thursday
Friday
End Enum 'Weekdays
2
Operators
VB.NET C#
'Comparison //Comparison
= < > <= >= <> == < > <= >= !=
'Arithmetic //Arithmetic
+ - * / + - * /
Mod % (mod)
\ (integer division) / (integer division if both operands are ints)
^ (raise to a power) Math.Pow(x, y)
'Assignment //Assignment
= += -= *= /= \= ^= <<= >>= &= = += -= *= /= %= &= |= ^= <<= >>= ++ --
'Bitwise //Bitwise
And AndAlso Or OrElse Not << >> & | ^ ~ << >>
'Logical //Logical
And AndAlso Or OrElse Not && || !
3
Choices
VB.NET C#
greeting = IIf(age < 20, "What's up?", "Hello") greeting = age < 20 ? "What's up?" : "Hello";
4
Loops
VB.NET C#
'Pre-test Loops: //Pre-test Loops: while (i < 10)
While c < 10 i++;
c += 1 for (i = 2; i < = 10; i += 2)
End While Do Until c = 10 System.Console.WriteLine(i);
c += 1
Loop
'Post-test Loop:
Do While c < 10
c += 1 //Post-test Loop:
Loop do
i++;
while (i < 10);
For c = 2 To 10 Step 2
System.Console.WriteLine(c)
Next
5
Arrays
VB.NET C#
Dim nums() As Integer = {1, 2, 3} int[] nums = {1, 2, 3};
For i As Integer = 0 To nums.Length - 1 for (int i = 0; i < nums.Length; i++)
Console.WriteLine(nums(i)) Console.WriteLine(nums[i]);
Next
'4 is the index of the last element, so it holds 5 // 5 is the size of the array
elements string[] names = new string[5];
Dim names(4) As String names[0] = "Steven";
names(0) = "Steven" // Throws System.IndexOutOfRangeException
'Throws System.IndexOutOfRangeException names[5] = "Sarah"
names(5) = "Sarah"
6
Functions
VB.NET C#
'Pass by value (in, default), reference // Pass by value (in, default), reference
'(in/out), and reference (out) //(in/out), and reference (out)
Sub TestFunc(ByVal x As Integer, ByRef y As void TestFunc(int x, ref int y, out int z) {
Integer, x++;
ByRef z As Integer) y++;
x += 1 z = 5;
y += 1 }
z = 5
End Sub
7
Exception Handling
VB.NET C#
Class Withfinally class Withfinally
Public Shared Sub Main() {
Try public static void Main()
Dim x As Integer = 5 {
Dim y As Integer = 0 try
Dim z As Integer = x / y {
Console.WriteLine(z) int x = 5;
Catch e As DivideByZeroException int y = 0;
System.Console.WriteLine("Error int z = x/y;
occurred") Console.WriteLine(z);
Finally }
System.Console.WriteLine("Thank you") catch(DivideByZeroException e)
End Try {
End Sub 'Main System.Console.WriteLine("Error occurred");
End Class 'Withfinally }
finally
{
System.Console.WriteLine("Thank you");
}
}
}
Namespaces
VB.NET C#
Namespace ASPAlliance.DotNet.Community namespace ASPAlliance.DotNet.Community {
... ...
End Namespace }
'or // or
8
Classes / Interfaces
VB.NET C#
'Accessibility keywords //Accessibility keywords
Public public
Private private
Friend internal
Protected protected
Protected Friend protected internal
Shared static
'Inheritance //Inheritance
Class Articles class Articles: Authors {
Inherits Authors ...
... }
End Class
using System;
Imports System
interface IArticle
Interface IArticle {
Sub Show() void Show();
End Interface 'IArticle }
_
class IAuthor:IArticle
Class IAuthor {
Implements IArticle public void Show()
{
Public Sub Show() System.Console.WriteLine("Show() method
System.Console.WriteLine("Show() method Implemented");
Implemented") }
End Sub 'Show
'Entry point which delegates to C-style main public static void Main(string[] args)
Private Function {
Public Overloads Shared Sub Main() IAuthor author = new IAuthor();
Main(System.Environment.GetCommandLineArgs()) author.Show();
End Sub }
}
9
Constructors / Destructors
VB.NET C#
Class TopAuthor class TopAuthor {
Private _topAuthor As Integer private int _topAuthor;
Objects
VB.NET C#
Dim author As TopAuthor = New TopAuthor TopAuthor author = new TopAuthor();
With author
.Name = "Steven" //No "With" construct
.AuthorRanking = 3 author.Name = "Steven";
End With author.AuthorRanking = 3;
author.Rank("Scott")
author.Demote() 'Calling Shared method author.Rank("Scott");
'or TopAuthor.Demote() //Calling static method
TopAuthor.Rank()
10
Structs
VB.NET C#
Structure AuthorRecord struct AuthorRecord {
Public name As String public string name;
Public rank As Single public float rank;
Public Sub New(ByVal name As String, ByVal public AuthorRecord(string name, float rank) {
rank As Single) this.name = name;
Me.name = name this.rank = rank;
Me.rank = rank }
End Sub }
End Structure
11
Properties
VB.NET C#
Private _size As Integer private int _size;
foo.Size += 1 foo.Size++;
12
Me.day = day }
Me.month = month
Me.year = year
End Sub 'SetDate
End Class '[Date]
Delegates / Events
VB.NET C#
Delegate Sub MsgArrivedEventHandler(ByVal message delegate void MsgArrivedEventHandler(string
As String) message);
'or to define an event which declares a //Delegates must be used with events in C#
'delegate implicitly
Event MsgArrivedEvent(ByVal message As String)
MsgArrivedEvent += new MsgArrivedEventHandler
(My_MsgArrivedEventCallback);
AddHandler MsgArrivedEvent, AddressOf //Throws exception if obj is null
My_MsgArrivedCallback MsgArrivedEvent("Test message");
'Won't throw an exception if obj is Nothing MsgArrivedEvent -= new MsgArrivedEventHandler
RaiseEvent MsgArrivedEvent("Test message") (My_MsgArrivedEventCallback);
RemoveHandler MsgArrivedEvent, AddressOf
My_MsgArrivedCallback
using System.Windows.Forms;
Imports System.Windows.Forms
Button MyButton = new Button();
MyButton.Click += new
'WithEvents can't be used on local variable System.EventHandler(MyButton_Click);
Dim WithEvents MyButton As Button
MyButton = New Button
private void MyButton_Click(object sender,
System.EventArgs e) {
Private Sub MyButton_Click(ByVal sender As MessageBox.Show(this, "Button was clicked",
System.Object, _ "Info",
ByVal e As System.EventArgs) Handles MessageBoxButtons.OK,
MyButton.Click MessageBoxIcon.Information);
MessageBox.Show(Me, "Button was clicked", "Info", }
_
MessageBoxButtons.OK,
MessageBoxIcon.Information)
End Sub
13
Console I/O
VB.NET C#
'Special character constants //Escape sequences
vbCrLf, vbCr, vbLf, vbNewLine \n, \r
vbNullString \t
vbTab \\
vbBack \
vbFormFeed
vbVerticalTab
"" Convert.ToChar(65) //Returns 'A' - equivalent
Chr(65) 'Returns 'A' to Chr(num) in VB
// or
(char) 65
System.Console.Write("What's your name? ")
Dim name As String = System.Console.ReadLine()
System.Console.Write("How old are you? ") System.Console.Write("What's your name? ");
Dim age As Integer = Val(System.Console.ReadLine()) string name = SYstem.Console.ReadLine();
System.Console.WriteLine("{0} is {1} years old.", System.Console.Write("How old are you? ");
name, age) int age =
'or Convert.ToInt32(System.Console.ReadLine());
System.Console.WriteLine(name & " is " & age & " System.Console.WriteLine("{0} is {1} years
years old.") old.", name, age);
//or
Dim c As Integer System.Console.WriteLine(name + " is " + age
c = System.Console.Read() 'Read single char + " years old.");
System.Console.WriteLine(c) 'Prints 65 if user
enters "A"
int c = System.Console.Read(); //Read single
char
System.Console.WriteLine(c); //Prints 65 if
user enters "A"
14
File I/O
VB.NET C#
Imports System.IO using System.IO;
'Read all lines from text file //Read all lines from text file
Dim reader As StreamReader = File.OpenText StreamReader reader = File.OpenText
("c:\myfile.txt") ("c:\\myfile.txt");
Dim line As String = reader.ReadLine() string line = reader.ReadLine();
While Not line Is Nothing while (line != null) {
Console.WriteLine(line) Console.WriteLine(line);
line = reader.ReadLine() line = reader.ReadLine();
End While }
reader.Close() reader.Close();
15