ComparisonofVBandC#.pdf

(100 KB) Pobierz
The Code Project - Complete Comparison for VB.NET and C# - .NET
C#, VB.NET
Windows, .NET
Win32, VS (VS.NET2003),
WinForms, WebForms
CEO, Arch, DB, Dev, QA
Posted 30 Mar 2005 14:53
Updated 18 Apr 2005 9:26
22,120 views
Complete Comparison for
VB.NET and C#
This article explains about advantages, differences and new
features of VB.NET and C#.
47 members have rated this article. Result:
Popularity: 7.49 . Rating: 4.48 out of 5.
Contents
1. Introduction
2. Advantages of both languages
3. Keyword Differences
4. Data types Differences
5. Operators Differences
6. Programming Difference
7. New Features of both languages in 2005 version
8. Conclusion
9. History
Introduction
Some people like VB.NET's natural language, case-insensitive approach, others like
C#'s terse syntax. But both have access to the same framework libraries. We will
discuss about the differences in the following topics:
1. Advantages of both languages
2. Keyword Differences
3. Data types Differences
4. Operators Differences
5. Programming Difference
Advantages of both languages
64997120.004.png 64997120.005.png
VB.NET
C#
l
Support for optional parameters - very
handy for some COM interoperability.
l
XML documentation generated
from source code comments. (This
is coming in VB.NET with Whidbey
(the code name for the next
version of Visual Studio and .NET),
and there are tools which will do it
with existing VB.NET code
already.)
Support for late binding with Option
Strict off - type safety at compile time
goes out of the window, but legacy
libraries which don't have strongly typed
interfaces become easier to use.
l
Support for named indexers.
l
Various legacy VB functions (provided in
the Microsoft.VisualBasic
namespace, and can be used by other
languages with a reference to the
Microsoft.VisualBasic.dll ). Many of these
can be harmful to performance if used
unwisely, however, and many people
believe they should be avoided for the
most part.
Operator overloading - again,
coming to VB.NET in Whidbey.
l
l
Language support for unsigned
types (you can use them from
VB.NET, but they aren't in the
language itself). Again, support for
these is coming to VB.NET in
Whidbey.
l
The using statement, which
makes unmanaged resource
disposal simple.
l
The with construct: it's a matter of
debate as to whether this is an
advantage or not, but it's certainly a
difference.
l
Explicit interface implementation,
where an interface which is
already implemented in a base
class can be re-implemented
separately in a derived class.
Arguably this makes the class
harder to understand, in the same
way that member hiding normally
does.
l
Simpler (in expression - perhaps more
complicated in understanding) event
handling, where a method can declare
that it handles an event, rather than the
handler having to be set up in code.
l
The ability to implement interfaces with
methods of different names. (Arguably
this makes it harder to find the
implementation of an interface,
however.)
l
l
Unsafe code. This allows pointer
arithmetic etc, and can improve
performance in some situations.
However, it is not to be used
lightly, as a lot of the normal
safety of C# is lost (as the name
implies). Note that unsafe code is
still managed code, i.e., it is
compiled to IL, JITted, and run
within the CLR.
Catch ... When ... clauses, which allow
exceptions to be filtered based on
runtime expressions rather than just by
type.
l
The VB.NET parts of Visual Studio .NET
compiles your code in the background.
While this is considered as an advantage
for small projects, people creating very
large projects have found that the IDE
slows down considerably as the project
gets larger.
l
64997120.006.png 64997120.007.png
Keyword Differences
Purpose
VB.NET
C#
Declare a variable
Private , Public , Friend ,
Protected , Static1 ,
Shared , Dim
declarators (keywords include
user-defined types and built-in
types)
Declare a named
constant
Const
const
Create a new object New , CreateObject()
new
Function/method
does not return a
value
Sub
void
Overload a function
or method (Visual
Basic: overload a
procedure or
method)
Overloads
(No language keyword required for
this purpose)
Refer to the current
object
Me
this
Make a nonvirtual
call to a virtual
method of the
current object
MyClass
n/a
Retrieve character
from a string
GetChar Function
[]
Declare a compound
data type (Visual
Basic: Structure)
Structure <members> End
Structure
struct , class , interface
Initialize an object
(constructors)
Sub New ()
Constructors, or system default type
constructors
Terminate an object
directly
n/a
n/a
Method called by the
system just before
garbage collection
reclaims an object7
Finalize
destructor
64997120.001.png
Initialize a variable
where it is declared
Dim x As Long = 5
// initialize to a value:
int x = 123 ;
// or use default
// constructor:
int x = new int ();
Dim c As New _
Car(FuelTypeEnum.Gas)
Take the address of
a function
AddressOf (For class
members, this operator
returns a reference to a
function in the form of a
delegate instance)
delegate
Declare that an
object can be
modified
asynchronously
n/a
volatile
Force explicit
declaration of
variables
Option Explicit
n/a. (All variables must be declared
prior to use)
Test for an object
variable that does
not refer to an
object
obj = Nothing
obj == null
Value of an object
variable that does
not refer to an
object
Nothing
null
Test for a database
null expression
IsDbNull
n/a
Test whether a
Variant variable has
been initialized
n/a
n/a
Define a default
property
Default
by using indexers
Refer to a base class MyBase
base
Declare an interface
Interface
interface
Specify an interface
to be implemented
Implements (statement) class C1 : I1
Declare a class
Class <implementation> class
64997120.002.png
Specify that a class
can only be
inherited. An
instance of the class
cannot be created.
MustInherit
abstract
Specify that a class
cannot be inherited
NotInheritable
sealed
Declare an
enumerated type
Enum <members> End Enum enum
Declare a class
constant
Const
const (Applied to a field
declaration)
Derive a class from
a base class
Inherits C2
class C1 : C2
Override a method
Overrides
override
Declare a method
that must be
implemented in a
deriving class
MustOverride
abstract
Declare a method
that can't be
overridden
NotOverridable (Methods
are not overridable by
default.)
sealed
Declare a virtual
method, property
(Visual Basic), or
property accessor
(C#, C++)
Overridable
virtual
Hide a base class
member in a derived
class
Shadowing
n/a
Declare a typesafe
reference to a class
method
Delegate
delegate
Specify that a
variable can contain
an object whose
events you wish to
handle
WithEvents
(Write code - no specific keyword)
Specify the events
for which an event
procedure will be
called
Handles (Event procedures
can still be associated with a
WithEvents variable by
naming pattern.)
n/a
64997120.003.png
Zgłoś jeśli naruszono regulamin