| This article is part of the Programming Language Comparison series. |
|---|
| General Comparison |
| Basic Syntax |
| Basic Instructions |
| Arrays |
| Associative arrays |
| String Operations |
| String Functions |
| List comprehension |
| Object-oriented programming |
| Database access |
|
|
| Evaluation strategy |
| List of "hello world" programs |
|
|
| Comparison of ALGOL 68 and C++ |
| Compatibility of C and C++ |
| Comparison of Pascal and Borland Delphi |
| Comparison of Pascal and C |
| Comparison of Object Pascal and C++ |
| Comparison of Java and C++ |
| Comparison of Java and C# |
| Comparison of C# and Visual Basic .NET |
| Comparison of ABAP and Java |
|
This box: view • talk • edit
|
|
|
This article or section is written like a personal reflection or essay and may require cleanup. Please help improve it by rewriting it in an encyclopedic style. (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.
Contents |
It should be noted that all .NET programming languages share the same runtime engine, and when compiled produce binaries that are seamlessly compatible with other .NET programming languages, including cross language inheritance, exception handling, and debugging.
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.
It should be noted that Visual Basic and Visual Basic .NET are completely different languages and are not backwards-compatible. Visual Basic .NET shares only syntax similarities with Visual Basic. The Abstract syntax tree for Visual Basic .NET and C# are closely matched and the IL produced by the compiler is virtually identical. It is possible to compile C# code into IL and use a decompiler to convert the compiled IL back into Visual Basic .NET source code - such is the similarity between the languages.
C# shares syntax similarities with Java. Both C# and Visual Basic .NET share structural similarities with other modern high level languages such as Java and C++. However the differences between Java and .NET are numerable, as can be seen in Comparison of Java and C Sharp.
Visual Studio provides minor differences in the development environment for C# and VB.Net. With the release of subsequent versions of Visual Studio the differences between the languages have reduced. For instance early versions of Visual Studio had poor support for intellisense in C# compared to Visual Basic .NET.
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.
These features are common to both C# and VB.Net as well as all languages running the .Net platform.
Handles constructunsafe code blocks for improved performance at the expense of not being verifiable as "safe" by the runtimeyield keywordchecked and unchecked contexts for fine-grained control of overflow/underflow checking=. Whereas C# has separate tokens, == for comparison and = to assign a value.variable1 can be different to Variable1. Visual Studio will correct the case of variables as they are typed in VB.NETVisual Basic .NET terminates a block of code with End BlockName or Next for a for loop which is more familiar for programmers with experience using T-SQL. In C#, the braces, {}, are use to delimit blocks, which is more familiar to programmers with experience in other modern languages such as C++ and Java.
In C#, when a block is not defined for a statement, the next statement will be treated as the block.
Sometimes C# developers will add comments at the end of scope to more easily mark where the scope started. For instance some C# developers use } // End If to mark the end of a nested if statement. This can negate the argument that C# is a cleaner language to read.
Keywords are very different between the two languages.
A comparison: (VB.NET vs C#)
Me vs this - a self-reference to the current object instanceMyBase vs base - for referring to the base class from which the current class is derivedShared vs static - for declaring methods that do not require an explicit instance of an objectNotInheritable vs sealed - for declaring classes that may not be inheritedNotOverridable vs sealed - for declaring methods that may not be overridden by derived classesMustInherit vs abstract - prevents a class from being directly instantiated, and forces consumers to create objects references to only derived classesMustOverride vs abstract - for forcing derived classes to override this methodOverridable vs virtual - declares a method as being able to be overridden in derived classesNotice above how some C# keywords such as sealed are used to represent different things when applied to method or class definitions. VB.NET, on the other hand, uses different keywords for different context
| C# | Visual Basic .NET |
|---|---|
// Single Line Comment
|
' Single Line Comment
|
/* Multi-line
comment */
|
not available |
/// Single Line XML Comment
|
''' Single Line XML Comment
|
/** Multi-line
XML comment */
|
not available |
Note that for Visual Basic .NET, multiple lines of code can be commented or uncommented using the Visual Studio IDE. As such having no support for Multi-line comments in the language is a disadvantage only when the IDE is not used.
| 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 |
| 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 |
| 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 |
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:
As counter examples
C++/CLI (a replacement for Managed Extensions for 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# 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#. J# is not included with Visual Studio 2008 and thus discontinued.
There are many additional languages available for the .NET Platform.
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.
|
||||||||||||||||||||||||||||||||||
Why are we here?
All text is available under the terms of the GNU Free Documentation License
This page is cache of Wikipedia. History