Comparison of C Sharp and Visual Basic .NET: Difference between revisions
Line 53: | Line 53: | ||
* Multi-line comments |
* Multi-line comments |
||
* Static classes (Classes which cannot contain any non-static members, although VB's Modules are essentially sealed static classes with additional semantics) |
* Static classes (Classes which cannot contain any non-static members, although VB's Modules are essentially sealed static classes with additional semantics) |
||
* |
* Can use <code>checked</code> and <code>unchecked</code> contexts for fine-grained control of overflow/underflow checking |
||
* Additional integral types: sbyte, ushort, uint, ulong |
* Additional integral types: <code>sbyte, ushort, uint, ulong</code> |
||
=== Features of Visual Basic .NET not found in C#=== |
=== Features of Visual Basic .NET not found in C#=== |
Revision as of 21:33, 7 January 2008
#
The original .NET Framework distributions from Microsoft included several language-to-IL compilers, including the two primary languages: C# and Visual Basic. There is heated debate among the greater .NET community about which language is better in general, or for specific purposes. Below is a comparison of the two languages.
Compatibility
It should be noted that all .NET programming languages share the same runtime engine, and when compiled produced binaries that are seamlessly compatible with other .NET programming languages, including cross language inheritance, exception handling, and debugging.
No second class languages
This article may need to be rewritten to comply with Wikipedia's quality standards. |
One of the main goals of .NET has been its multi language support, and the concept of "no second class languages". That is, all of the various Microsoft languages should have the same level of access to all OS features, and all expose the same level of power and usability.
Visual Basic has been significantly updated compared to VB versions 1 through 6. Older versions of VB were object-based, that is to say not fully object-oriented, had limited access to API functions through COM, and had many other limitations. In .NET, most of these limitations have been removed, with minimal impact on the language's syntax and structure. VB.NET is fully object-oriented, and has full access to the .NET framework.
C# has taken concepts from other languages, notably Java and Delphi, combined with a syntax similar to C and Java.
Culture
While there are some technical differences between the languages, there are also many differences in the cultures of the developers who use them. .NET developers have come from many different programming backgrounds, each having very different coding standards, best practices, etc. Many of these paradigms came from language or IDE features. While the historical reasons for these practices are no longer applicable in all cases, the culture differences remain.
Visual Basic .NET culture
VB historically was a RAD tool, as well as a tool that was usable by untrained developers, or management. As a result of this wider user base, VB allowed for many shortcuts and ease of use features. These features allowed for the uninitiated to create code, but also contributed to maintenance and debugging issues. Examples of the shortcuts allowed in VB
- Optional declaration of variables
- Optional Weak Typing using late binding
- Optional Parameters
C# culture
C# developers tend to be prior C/C++ developers, or Java developers. C# is syntactically very similar to C and Java, the IDEs and command line tools work very similarly, and the culture is the same. Also, C# inherited quite a bit of its style and culture from the Delphi/Object Pascal programming language
Development Environment
Visual Studio 2005 provides minor differences in the development environment for C# and VB.Net.
Exclusive C# Development Environment Features
- Exceptions that are documented in XML comments are displayed by Intellisense
- Intellisense auto-completes the names of in-scope variables
Exclusive VB.Net Development Environment Features
- Auto-complete corrects the casing of variables
- Default namespace is hidden
- Certain project files are hidden (user can show them)
Language features
The bulk of the differences between C# and VB.NET from a technical perspective are syntactic sugar. That is, most of the features are in both languages, but some things are easier to do in one language than another. Many of the differences between the two languages are actually centered around the IDE.
Features common to all .Net languages
These features are common to both C# and VB.Net as well as all languages running the .Net platform.
- Garbage collection
Features of C# not found in Visual Basic .NET
- Supports
unsafe
code blocks for improved performance at the expense of not being verifiable as "safe" by the runtime - Anonymous methods
- Iterators
- Multi-line comments
- Static classes (Classes which cannot contain any non-static members, although VB's Modules are essentially sealed static classes with additional semantics)
- Can use
checked
andunchecked
contexts for fine-grained control of overflow/underflow checking - Additional integral types:
sbyte, ushort, uint, ulong
Features of Visual Basic .NET not found in C#
With
keyword for using the same object repeatedly (this feature was intentionally not added to C# [1])- Named indexers (essentially, properties that take arguments)
- The
Handles
keyword allows declarative wiring of events to functions; Imperative (C# style) event wiring is also available. - Optionally ignore ref/ByRef behavior for passing arguments. (C# requires a temp variable to do this.)
- Optional parameter support is useful when using COM automation. This is especially important when working with Microsoft Office.
- VB Select Case (switch statements) allow for ranges and even mathematical operations on each condition.
- The
MyClass
keyword which performs a non-virtual method call on an overridden method - In VB functions, a variable of the same name and type is implicitly created. For example, in a boolean function named DoSomething(), this code is valid:
DoSomething = True
;[2] the variableDoSomething
may even be passed by reference to another routine and modified by it:dosomethingElse(DoSomething)
. In C#, the latter would require 3 statementsbool retval = true; dosomethingElse(ref retval); return retval;
.
Criticisms of Visual Basic .NET not applicable to C#
VB.NETs supports several syntactic "shortcuts", which exist mostly due to the language's legacy support. While these can be valuable in some circumstances, they are also often a significant source of errors and headaches.
For example, Visual Basic .NET has support for the legacy MsgBox()
in addition to the fully-qualified .NET call of System.Windows.Forms.MessageBox.Show()
- Optional variable declaration
- Optional strong typing
- Conversion of Boolean value True to Integer may yield -1 or 1 depending on the conversion used
Criticisms of C# not applicable to Visual Basic .NET
- By default, numeric operations are not checked. This results in slightly faster code, at the risk that numeric overflows will not be detected. However, the programmer can place arithmetic operations into a checked context to activate overflow checking.
- Lack of optional parameters in functions, a feature present in both Visual Basic.NET and C++ (This is done on purpose to force the programmer to use Polymorphism and/or Generics).
Syntax comparisons
One of the large differences is in blocks. VB terminates a block of code with End BlockName
, for example:
While something
End While
In C# the use of braces, {}
, to delimit blocks will be more familiar to programmers with experience in other modern languages.
Keywords
Keywords are very different between the two languages.
A comparison: (VB vs C#)
Me
vsthis
- a self-reference to the current object instanceMyBase
vsbase
- for referring to the base class from which the current class is derivedShared
vsstatic
- for declaring methods that do not require an explicit instance of an objectNotInheritable
vssealed
- for declaring classes that may not be inheritedNotOverridable
vssealed
- for declaring methods that may not be overridden by derived classesMustInherit
vsabstract
- prevents a class from being directly instantiated, and forces consumers to create objects references to only derived classesMustOverride
vsabstract
- for forcing derived classes to override this methodOverridable
vsvirtual
- declares a method as being able to be overridden in derived classes
Notice above how some C# keywords such as sealed
are used to represent different things when applied to method or class definitions. VB, on the other hand, uses different keywords for different contexts.
Comments
C# | Visual Basic .NET |
---|---|
// Single Line Comment | ' Single Line Comment |
/* Multi-line
comment */ |
N/A |
/// XML Comments | ''' XML Comments |
if-then-else
C# | Visual Basic .NET |
if (condition)
{
// condition is true
}
|
If condition Then
'condition is true
End If
|
if (condition)
{
// condition is true
}
else
{
// condition is false
}
|
If condition Then
'condition is true
Else
'condition is false
End If
|
if (condition)
{
// condition is true
}
else if (othercondition)
{
// condition is false and othercondition is true
}
|
If condition Then
'condition is true
ElseIf othercondition Then
'condition is false and othercondition is true
End If
|
For loop
C# | Visual Basic .NET |
for (int i = 0; i < number; i++)
{
// Loop from zero up to one less than number
}
|
For i As Integer = 0 To number - 1
' Loop from zero up to one less than number
Next
|
for (int i = number; i >= 0; i--)
{
// Loops from number down to zero
}
|
For i As Integer = number To 0 Step -1
' Loops from number down to zero
Next
|
Equality and other comparison operators
C# | Visual Basic .NET | |
if (a == b)
{
// equal
}
|
If a = b Then
' equal
End If
| |
if (a != b)
{
// not equal
}
|
If a <> b Then
' not equal
End If
|
|
if ((a == b) & (c == d) | (e == f))
{
// multiple comparisons
}
|
If a = b And c = d Or e = f Then
' multiple comparisons
End If
|
|
if ((a == b) && (c == d) || (e == f))
{
// short-circuiting comparisons
}
|
If a = b AndAlso c = d OrElse (e = f) Then
' short-circuiting comparisons
End If
|
Performance
Adoption and community support
Both C# and VB.Net have high adoption rates, and very active developer communities and Microsoft fully supports both communities. However, C# does have an advantage in terms of the level of community activity on the Internet. Also, there are more books available for C# than VB.Net, and publishers report that C# books significantly outsell the VB.Net counterparts.
Examples of the increased community and industry adoption include:
- According to a survey conducted by Visual Studio Magazine (which self-identifies as a VB centric publication) "41 percent said they used C#, 34 percent programmed in VB.NET, while 25 percent responded with 'other.'" [1]
- Stephen Wiley, marketing product manager at Apress has reported "C# titles outsell VB.NET title books handily, by somewhere between a 2-1 and 3-1 margin."[1]
- blogs.msdn.com, the blogging site for Microsoft employees, has 27,500 posts that discuss C#, while only 8,880 mention VB.Net (November 15, 2007)
- Google Groups, a Usenet search engine returns 36,900 hits for "VB .Net", and 65,700 for C#
- Amazon returns 9,923 hits for C#, and 2,149 hits for "Visual Basic .Net" (November 15, 2007).
As counter examples
- VB.NET is the most popular download of all the Express downloads. C++ is the most popular download among students. 80 percent of all downloads for the Express line were by non-professionals. [1]
Other languages
C++/CLI (formerly Managed C++)
C++/CLI (a replacement for Managed C++) does not have the adoption rate of C# or VB.NET, but does have a significant following. C++/CLI syntactically, stylistically, and culturally is closest to C#. However, C++/CLI stays closer to its C++ roots than C# does. C++/CLI directly supports pointers, deconstructors, and other unsafe program concepts which are not supported or limited in the other languages. It allows the direct use of both .NET code and standard C++ code. C++/CLI is used for porting native/legacy C++ applications into the .NET framework, or cases where the programmer wants to take more control of the code; but this control comes at a significant cost of ease of use and readability. Many of the automated tools that come with Visual Studio have reduced functionality when interacting with C++ code. This is because reflection cannot provide as much information about the code as it can for C# and VB.net
J#
J# runs a distant fourth in terms of adoption. J# is a language primarily designed to ease the transition of Java applications to the .NET framework; it allows developers to leave much of their Java or J++ code unchanged while still running it in the .NET framework, thus allowing them to migrate small pieces of it into another .NET language, such as C#, individually. J# does not receive the same level of updates as the other languages, and does not have the same level of community support. For example, Visual Studio 2005 supports automatic generation of Unit Tests in C#, VB.Net, and C++, but excludes J#. Microsoft recommends against developing new applications in J#.
Minor languages
There are many additional languages available for the .NET Platform.
CIL
All .NET languages compile down to Common Intermediate Language (CIL) , but it is also possible to code directly in CIL. This can be done for performance or security reasons, or just for fun. It is a common practice to make source changes in the original C# or VB.NET, and then compare the resulting CIL, to see what benefits or consequences might result [verification needed]. Coding directly in CIL often makes code that is difficult or impossible to de-compile to a higher level language like C#. Either the decompile fails, or the resulting code is not very readable. This is analogous to writing directly in assembly language, and then decompiling to C++. This technique is used by many obfuscators to help prevent reverse-engineering. It is possible to code an entire application directly in CIL, but this would be very cumbersome.
References
- ^ a b c C#'s Exploding Mindshare, Visual Studio Magazine, November 2007