Comparison of C# and VB.Net

Download Report

Transcript Comparison of C# and VB.Net

Comparison of
C# and VB.Net
By
Sam Nasr
October 26, 2004
What is C#?

C# (C-Sharp) is a new language developed by
Microsoft Corporation.

Designed by Anders Hejlsberg (creator of Turbo Pascal
and architect of Delphi), Scott Wiltamuth, and Peter
Golde.

Described as a “...simple, modern, object-oriented, and
type-safe programming language derived from C and
C++”.

Bears many syntactic similarities to C++ and Java.
What is VB.Net?

VB.Net is the latest release of Microsoft’s Visual Basic
language.

VB is an event driven programming language.

Derived heavily from BASIC.

VB enables Rapid Application Development (RAD) of
graphical user interface (GUI) applications.
Myths





VB.Net is intended for use with Microsoft
Office.
C# is the latest release of C++.
C# is easier to learn than VB.Net.
To better understand C#, one should first learn
C++.
A .Net solution can contain only 1 language.
Language Differences
The differences of the 2 languages lie in:
1.
2.
3.
Syntax
Object Oriented Features
Visual Studio.Net IDE
Syntax Differences














Case Sensitivity
Line Termination
Comments
Namespace Declaration and Usage
Variable Declaration
Variable Initialization
Declaring Function Parameters
Passing Function Parameters
Optional Parameters
Parameter Lists
Method Declaration
Returning Output Parameters
Program Startup
Exiting Programs/Methods/Loops














Member Scope
Static/Shared Methods
Classes versus Modules
If statements
Short Circuiting
Conditional Statements
Properties and Indexers
Arrays
For Loops
For/Each Loops
Try/Catch Blocks
Attribute Usage
Control Characters
Type Comparison and Conversion
C# Example
(Syntax)
/* C Style comment */
// C++ Style comment
///Enhanced comment for Documentation Feature
Void SampleProc()
{
int intCounter1=0;
MessageBox.show(“Counter1=” + intCounter1.ToString());
} //end of SampleProc
VB.Net Example
(Syntax)
‘Style of comments for VB.Net
Sub SampleProc()
Dim intCounter1 as Integer, intCounter2 as Integer
MessageBox.show(“Counter1=” & intCounter1)
End Sub ‘end of SampleProc
Object-Oriented Features







Inheritance Syntax
Method Overloading
Constructors
Invoking Other
Constructors
Invoking Base Constructors
Initializers
Hiding Base Class Members






Overriding methods
Requiring/Preventing
Inheritance
Declaring and
Implementing Interfaces
Delegated and Events
Comparing Classes
String Comparisons
C# Example
(Object Oriented)
class App
{
static void Main(string [ ] args)
{
int intCounter=0;
foreach (string arg in args)
{
System.Console.WriteLine(“Counter:” + intCounter.ToString() + “=“ + arg);
} //end of foreach
} //end of Main()
} //end of App{}
VB.Net Example
(Object Oriented)
Class App
Shared Sub Main(ByVal args as String( ) )
Dim arg as String
Dim intCounter as Integer
For Each arg in args
System.Console.Writeline(“Counter: ” & intCounter & “=“ & arg)
Next ‘For Each loop
End Sub ‘end of Main()
End Class ‘end of App{}
IDE Differences





AssemblyInfo.x
Default/Root Namespace
Startup object
App.ico
Imports/Using




COM References
Compiler Constants
Options:
(Explicit, Strict, Compare)
Errors and Warnings
Specific Language Features





Unsafe Blocks (C#)
Using (C#)
Documentation Comments (C#)
Operator Overloading (C#)
Late Binding (VB)
Unsafe Blocks (C#)
class Class1
{
static unsafe void main (string[ ] args)
{
string sName = “Joe Mojica”;
fixed (char *Temp = sName)
{
char *ch= Temp;
ch +=4;
*ch = (char)o;
ch += 1;
*ch = (char) o;
}
System.Console.WriteLine(sName);
}
}
Using (C#)
void OpenDatabase()
{
string cstr;
cstr = "Provider=Microsoft.Jet.OLEDB.4.0";
cstr += "Data Source=c:\\MyDB.mdb;";
using (OleDBConnection conn = new OleDBConnection(Cstr))
{
conn.open();
}
}
Documentation Comments (C#)
///<summary>
///A Sample Function to demonstrate C#
///</summary>
///<param name=“void”>This function does not receive a
parameter. </param>
///<returns>Void Type</returns>
Void SampleProc()
{
int intCounter1, intCounter2;
MessageBox.show(“Counter1=” & intCounter1);
} //end of SampleProc
Controversial Issues
Point
 C# offers less typing.
 C# is a better programming language because it
forces variables to be defined.
 C# code runs faster.
Counter-Point
 VB.Net offers automatic case setting
 VB.Net offers Option Explicit
 Speed is debatable!
Which Language do I choose for my
organization?






Which language features do I need?
Which language features can I live without?
What is the background of the staff ?
Which language does my staff work more
comfortably with?
Can staff members be comfortable with both?
What is the availability of Human Resources for
a specific language?
Language Converters
C# to VB.NET


http://www.aspalliance.com/aldotnet/examples/translate.aspx
http://www.kamalpatel.net/ConvertCSharp2VB.aspx
VB.Net to C#


http://www.e-iceblue.com
http://www.vbconversions.com
Additional Information

C# & VB.NET Conversion Pocket
Reference by Jose Mojica

C# in a Nutshell By Peter Drayton,
Ben Albahari, Ted Neward

http://en.wikipedia.org/wiki/Visual_Basic_pro
gramming_language
Upcoming Events

MSDN Event (http://msevents.microsoft.com)
Thursday (10/28/2004), 1:00 – 5:00 PM at Regal
Town Cinemas. Free

New SQL Server SIG
Every 3rd Thursday 6:30-8:30 PM
ALG - 30640 Bainbridge Road in Solon

Next meeting: COM and .Net Components
Thursday, Nov 23, 2004 6:30-8:30 PM
Career Info

Intuit Corp is seeking 2 Sr. .Net Developers. Contact Heather Winchester
([email protected]) at 214-387-2125. See
www.intuit.com/about_intuit/careers for more info.

Adaptive Solutions Group is seeking several C# web developers for contract to hire.
Contact Matt Twyman ([email protected]) at 314-236-3853. See
www.adaptivesg.com for more info.

Robert Half Technology is accepting resumes for various development positions.
Contact Kim Nasierowski ([email protected]) at 216-621-6633. See
www.rht.com for more info.

Tek Systems is accepting resumes for various development positions. Contact Janelle
Capretta ([email protected]) at 216-654-1840. See
http://www.teksystems.com for more info.

Invesmart is seeking 2 VB.Net developer positions. See www.invesmart.com