Documente Academic
Documente Profesional
Documente Cultură
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#
'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 {
'Entry point which delegates to C-style main class HelloWorld
Private Function {
Public Overloads Shared Sub Main() static void Main(string[] args)
Main(System.Environment.GetCommandLineArgs()) {
End Sub System.Console.WriteLine("Hello
World")
Overloads Shared Sub Main(args() As String) }
System.Console.WriteLine("Hello World") }
End Sub 'Main }
End Class 'HelloWorld End Namespace 'MyNameSpace
Data Types
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
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
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};
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
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)
'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
Operators
VB.NET C#
'Comparison //Comparison
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
= < > <= >= <> == < > <= >= !=
'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
+
Choices
VB.NET C#
greeting = IIf(age < 20, "What's up?", "Hello") greeting = age < 20 ? "What's up?" :
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
"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#
'Pre-test Loops: //Pre-test Loops: while (i < 10)
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
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
Arrays
VB.NET C#
Dim nums() As Integer = {1, 2, 3} int[] nums = {1, 2, 3};
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
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"
Functions
VB.NET C#
'Pass by value (in, default), reference // Pass by value (in, default), reference
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
'(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#
Class Withfinally class Withfinally
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
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 occurred") int z = x/y;
Finally Console.WriteLine(z);
System.Console.WriteLine("Thank you") }
End Try catch(DivideByZeroException e)
End Sub 'Main {
End Class 'Withfinally System.Console.WriteLine("Error
occurred");
}
finally
{
System.Console.WriteLine("Thank
you");
}
}
}
Namespaces
VB.NET C#
Namespace ASPAlliance.DotNet.Community namespace ASPAlliance.DotNet.Community {
... ...
End Namespace }
'or // or
Classes / Interfaces
VB.NET C#
'Accessibility keywords //Accessibility keywords
Public public
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
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 }
}
Overloads Public Shared Sub Main(args() As
String)
Dim author As New IAuthor()
author.Show()
End Sub 'Main
End Class 'IAuthor
Constructors / Destructors
VB.NET C#
Class TopAuthor class TopAuthor {
Private _topAuthor As Integer private int _topAuthor;
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
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()
Structs
VB.NET C#
Structure AuthorRecord struct AuthorRecord {
Public name As String public string name;
Public rank As Single public float rank;
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
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#
Private _size As Integer private int _size;
foo.Size += 1 foo.Size++;
Delegates / Events
VB.NET C#
Delegate Sub MsgArrivedEventHandler(ByVal message delegate void MsgArrivedEventHandler(string
As String) message);
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#
'Special character constants //Escape sequences
vbCrLf, vbCr, vbLf, vbNewLine \n, \r
vbNullString \t
vbTab \\
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
vbBack \
vbFormFeed
vbVerticalTab
"" Convert.ToChar(65) //Returns 'A' -
Chr(65) 'Returns 'A' equivalent 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 " +
c = System.Console.Read() 'Read single char age + " 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"
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();