CheatSheet C# Vs VBdoc PDF
CheatSheet C# Vs VBdoc PDF
CheatSheet C# Vs VBdoc PDF
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
Comments
VB.NET C#
Program Structure
VB.NET C#
Data Types
VB.NET C#
Constants
VB.NET C#
Enumerations
VB.NET C#
Enum Status
Flunk = 50
Pass = 70
Excel = 90
End Enum
'Prints 70 // Prints 70
System.Console.WriteLine(Status.Pass) System.Console.WriteLine((int)
'Prints Pass Status.Pass);
System.Console.WriteLine(Status.Pass.ToString()) // Prints Pass
System.Console.WriteLine(Status.Pass);
Enum Weekdays
Saturday
Sunday enum Weekdays
Monday {
Tuesday Saturday, Sunday, Monday, Tuesday,
Wednesday Wednesday, Thursday, Friday
Thursday }
Friday
End Enum 'Weekdays
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
Operators
VB.NET C#
'Comparison //Comparison
= < > <= >= <> == < > <= >= !=
'Arithmetic //Arithmetic
+ - * / + - * /
Mod % (mod)
\ (integer division) / (integer division if both operands are
^ (raise to a power) ints)
Math.Pow(x, y)
'Assignment
= += -= *= /= \= ^= <<= >>= &= //Assignment
= += -= *= /= %= &= |= ^= <<=
>>= ++ --
'Bitwise
And AndAlso Or OrElse Not << >>
//Bitwise
& | ^ ~ << >>
'Logical
And AndAlso Or OrElse Not
//Logical
&& || !
'String Concatenation
&
//String Concatenation
+
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
Choices
VB.NET C#
greeting = IIf(age < 20, "What's up?", "Hello") greeting = age < 20 ? "What's up?" :
"Hello";
'Preferred
If x <> 100 And y < 5 Then
x *= 5 if (x != 100 && y < 5)
y *= 2 {
End If // Multiple statements must be enclosed
in {}
x *= 5;
y *= 2;
}
'or to break up any long single command use _
If henYouHaveAReally < longLine And _
itNeedsToBeBrokenInto2 > Lines Then _
UseTheUnderscore(charToBreakItUp)
If x > 5 Then
x *= y
ElseIf x = 5 Then if (x > 5)
x += y x *= y;
ElseIf x < 10 Then else if (x == 5)
x -= y x += y;
Else else if (x < 10)
x /= y x -= y;
End If else
x /= y;
Loops
VB.NET C#
'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
Arrays
VB.NET C#
'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"
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, {
ByRef z As Integer) x++;
x += 1 y++;
y += 1 z = 5;
z = 5 }
End Sub
Dim total As Integer = Sum(4, 3, 2, 1) 'returns 10 int total = Sum(4, 3, 2, 1); // returns 10
Exception Handling
VB.NET C#
Namespaces
VB.NET C#
'or // or
Classes / Interfaces
VB.NET C#
'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 }
}
Constructors / Destructors
VB.NET C#
Objects
VB.NET C#
author.Rank("Scott")
author.Demote() 'Calling Shared method author.Rank("Scott");
'or TopAuthor.Demote() //Calling static method
TopAuthor.Rank()
Structs
VB.NET C#
Public Sub New(ByVal name As String, ByVal rank public AuthorRecord(string name, float
As Single) rank) {
Me.name = name this.name = name;
Me.rank = rank this.rank = rank;
End Sub }
End Structure }
Properties
VB.NET C#
foo.Size += 1 foo.Size++;
Delegates / Events
VB.NET C#
MsgArrivedEvent += new
AddHandler MsgArrivedEvent, AddressOf MsgArrivedEventHandler
My_MsgArrivedCallback (My_MsgArrivedEventCallback);
'Won't throw an exception if obj is Nothing //Throws exception if obj is null
RaiseEvent MsgArrivedEvent("Test message") MsgArrivedEvent("Test message");
RemoveHandler MsgArrivedEvent, AddressOf MsgArrivedEvent -= new
My_MsgArrivedCallback MsgArrivedEventHandler
(My_MsgArrivedEventCallback);
Imports System.Windows.Forms
using System.Windows.Forms;
Console I/O
VB.NET C#
File I/O
VB.NET C#
'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();