0% found this document useful (0 votes)
500 views10 pages

Comparison of C Sharp and Visual Basic

The document compares C# and Visual Basic .NET programming languages. It discusses that while both languages can be used to program on the .NET framework, they have some differences in features, syntax, and development environments. Key differences include optional parameters and auto-implemented properties in C# versus events handling in Visual Basic .NET.

Uploaded by

sanchi.varma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
500 views10 pages

Comparison of C Sharp and Visual Basic

The document compares C# and Visual Basic .NET programming languages. It discusses that while both languages can be used to program on the .NET framework, they have some differences in features, syntax, and development environments. Key differences include optional parameters and auto-implemented properties in C# versus events handling in Visual Basic .NET.

Uploaded by

sanchi.varma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Comparison of C Sharp and Visual Basic .

NET
C# and Visual Basic are the two primary languages used to program on the .NET Framework.

Contents
[hide]

1 Language Compatibility 2 Runtime multi-language support 3 Development environment 4 Language features o 4.1 Features of Visual Basic .NET not found in C# o 4.2 Features of C# not found in Visual Basic .NET o 4.3 Other characteristics of Visual Basic .NET not applicable to C# o 4.4 Other characteristics of C# not applicable to Visual Basic .NET 5 Syntax comparisons o 5.1 Keywords o 5.2 Comments o 5.3 Conditionals o 5.4 Loops o 5.5 Comparers 5.5.1 Primitive types 5.5.2 Object types o 5.6 Miscellaneous programming features 6 Adoption and community support 7 Other languages o 7.1 C++/CLI (formerly Managed C++) o 7.2 J# o 7.3 Additional .NET languages 8 References 9 External links

Language Compatibility
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. However some features of Visual Basic have been ported over to Visual Basic .NET and are not available to other .NET 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 numerous, as can be seen in Comparison of Java and C Sharp.

[edit] Runtime multi-language support


One of the main goals of .NET has been its multi-language support. The intent of the design was that all of the various Microsoft languages should have the same level of access to all OS features, should be able to expose the same level of power and usability, and simplify calling from a module in one language to that written in another language. In implementation, all .NET programming languages share the same runtime engine, uniform Abstract syntax tree, and Common Intermediate Language. Additionally all .NET languages have access to platform features including garbage collection, cross language inheritance, exception handling, and debugging. This allows the same output binary to be produced from any .NET programming language.

[edit] Development environment


Visual Studio provides minor differences in the development environment for C# and VB.Net. With each subsequent release of Visual Studio, the differences between development environments for these languages have been reduced. For instance early versions of Visual Studio had poor support for Intellisense in C# compared to Visual Basic .NET, and did not offer background compilation for C#[1]. Currently, the main differences in the development environments are additional features for Visual Basic .NET that originated in VB6, including:

The default namespace is hidden (but can be disabled) Certain project files are hidden (the user can show them) The auto-generated My.* namespaces contain many commonly-used shortcuts brought over from VB6, such as methods for operating on the registry and application configuration file

Background compilation is a feature of the Visual Studio IDE whereby code is compiled as it is written by the programmer with the purpose of identifying compilation errors without requiring the solution to be built. This feature has been available for Visual Basic since .NET 1.1 and was present in early versions of Visual Studio for Visual Basic .NET. However, background compilation is a relatively new concept for Visual C# and is available with service pack 1 for Visual Studio 2008 Standard Edition and above. A distinct disadvantage for C# is that the Error List panel does not update until the solution is rebuilt. Refactoring large projects in C# is made more difficult by the need to frequently rebuild the solution order to highlight compilation errors[2]. Such is not the case with Visual Basic because the Error List panel is synchronised with the background compiler.

Background Compilation is less demanding on system resources and results in faster build cycles[3]. This is a particular advantage with large projects and can significantly reduce the time required to start debugging the IDE[4].

[edit] 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.

[edit] Features of Visual Basic .NET not found in C#

Variables can be declared using the WithEvents construct. This construct is available so that a programmer may select an object from the Class Name drop down list and then select a method from the Declarations drop down list to have the Method signature automatically inserted Auto-wireup of events, VB.NET has the Handles syntax for events Support for optional parameters (this will also be available in C# beginning in version 4.0[5]). This is typically used to provide easy interoperability with native code Marshalling an object for multiple actions using an unqualified dot reference. This is done using the With ... End With structure IsNumeric evaluates whether a string can be cast into a numeric value (the equivalent for C# requires using int.TryParse) XML Literals[6] Inline date declarations by using #1/1/2000# syntax (M/dd/yyyy).

[edit] Features of C# not found in Visual Basic .NET


Allows blocks of unsafe code (like C++/CLI) via the unsafe keyword. Partial Interfaces Iterators and the yield keyword Multi-line comments (note that the Visual Studio IDE supports multi-line commenting for Visual Basic .NET) 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 and unchecked contexts for fine-grained control of overflow/underflow checking Auto-Implemented Properties (as of C# 3.0[7]) (This will be available in Visual Basic .NET beginning in version 10.) Implicitly Typed Arrays

[edit] Other characteristics of Visual Basic .NET not applicable to C#


Conversion of Boolean value True to Integer may yield -1 or 1 depending on the conversion used Assigning and comparing variables uses the same token, =. Whereas C# has separate tokens, == for comparison and = to assign a value. VB.NET is not case-sensitive. Type checking is less strict by default. If the default is left in place, It will auto convert type without notifying programmer, for example:

Dim i As Integer = 1 Dim j As String = "1" If i = j Then MessageBox.Show("Bad comparison") End If

It should be noted that changing this default (known as 'Option Strict') is widely considered[by whom?] best practice for VB development.
VAL() function which also parses NULL value while converting into double (In c# Convert.ToDouble() is used to convert any object into double type value, but which throws exception in case of NULL value).

[edit] Other characteristics 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. (It can be done in Visual Basic by checking an option) Addition and string concatenation use the same token, +. Visual Basic .NET, however, has separate tokens, + for addition and & for concatenation, although + can be used for concatenation as well. In Visual Basic .NET property methods may take parameters C# is case-sensitive.

[edit] Syntax comparisons


Visual Basic .NET terminates a block of code with End BlockName statements (or Next statements, for a for loop) which are 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 widely-deployed languages such as C++ and Java. Additionally, in C# if a block consists of only a single statement, the braces may be omitted. C# is case sensitive while Visual Basic .NET is not. Thus in C# it is possible to have two variables with the same name, for example variable1 and Variable1. Visual Studio will correct the case of variables as they are typed in VB.NET. In many cases however,

case sensitivity can be useful. C# programmers typically capitalize type names and leave member and variable names lowercase. This allows, for example, fairly natural naming of method arguments: public int CalculateOrders(Customer customer). Of course, this can cause problems for those converting C# code to a case-insensitive language, such as Visual Basic, or to those unaccustomed to reading a case sensitive language.

[edit] Keywords
Keywords are very different between the two languages.
Me vs this - a self-reference to the current object instance MyBase vs base - for referring to the base class from which

the current class is

derived vs static - for declaring methods that do not require an explicit instance of an object NotInheritable vs sealed - for declaring classes that may not be inherited NotOverridable vs sealed - for declaring methods that may not be overridden by derived classes MustInherit vs abstract - prevents a class from being directly instantiated, and forces consumers to create object references to only derived classes MustOverride vs abstract - for forcing derived classes to override this method Overridable vs virtual - declares a method as being able to be overridden in derived classes
Shared

Some C# keywords such as sealed represent different things when applied to methods as opposed to when they are applied to class definitions. VB.NET, on the other hand, uses different keywords for different contexts.

[edit] Comments
C# Visual Basic .NET
/** Multi-line 'Single line comments only XML comment */ 'Multi-line comments are not available // Single line comment

[edit] Conditionals
C#
if (condition) { // condition is true } if (condition) { // condition is true } else { // condition is false }

Visual Basic .NET


If condition Then ' condition is true End If 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) { // condition is true } else if (othercondition) { // condition is false and othercondition is true } else { // condition and othercondition are false }

If condition Then ' condition is true ElseIf othercondition Then ' condition is false and othercondition is true End If

If condition Then ' condition is true ElseIf othercondition Then ' condition is false and othercondition is true Else ' condition and othercondition false End If

[edit] Loops
C#
for (int i = 0; i < number; i++) { // loop from zero up to one less than number } for (int i = number; i >= 0; i--) { // loops from number down to zero } break; //breaks out of a loop

Visual Basic .NET


For i As Integer = 0 To number - 1 ' loop from zero up to one less than number Next i For i As Integer = number To 0 Step -1 ' loops from number down to zero Next i Exit For 'breaks out of a for loop Exit While 'breaks out of a while loop Exit Do 'breaks out of a do loop

[edit] Comparers

[edit] Primitive types


C#
if (a == b) { // equal } if (a != b) { // not equal }

Visual Basic .NET


If a = b Then ' equal End If If a <> b Then ' not equal End If

Or:
if !(a == b) { // not equal } if ((a == b) & (c == d) | (e == f)) { // multiple comparisons } if ((a == b) && (c == d) || (e == f)) { // short-circuiting comparisons }

Or:
If Not a = b Then ' not equal End If If a = b And c = d Or e = f Then ' multiple comparisons End If If a = b AndAlso c = d OrElse (e = f) Then ' short-circuiting comparisons End If

[edit] Object types


C#
if (Object.ReferenceEquals(a, b)) { // variables refer to the same instance } if (!Object.ReferenceEquals(a, b)) { // variables do not refer to the same instance } if (a.Equals(b)) { // instances are equivalent } if ( ! a.Equals(b)) { // not equivalent } var type = typeof(int); if (a is b) { // types of a and b are compatible } if (!(a is b)) { // types of a and b are not compatible }

Visual Basic .NET


If a Is b Then ' variables refer to the same instance End If If a IsNot b Then ' variables do not refer to the same instance End If If a = b Then ' instances are equivalent End If If a <> b Then ' not equivalent End If Dim type = GetType(Integer) If TypeOf a Is b Then ' types of a and b are compatible End If If Not TypeOf a Is b Then ' types of a and b are not compatible End If

Note: these examples for equivalence tests assume neither the variable "a" nor the variable "b" is a Null reference (Nothing in Visual Basic.NET). If "a" were null, the C#

evaluation of the .equals method would throw a NullReferenceException, whereas the VB.NET = operator would return true if both were null, or false if only one was null (and evaluate the equals method if neither were null). They also assume that the .equals method and the = operator are implemented for the class type in question. Omitted for clarity, the exact transliteration would be: C#
if ((a == null && b == null) || (a != null && b != null) && a.equals(b))

VB.NET
If a = b Then

[edit] Miscellaneous programming features


C#
a = a = int var x ? y : z; y ?? z; MyInt { get; set; } x = from n in numbers where n > 0 orderby n select n;

Visual Basic .NET


a = If(x, y, z) a = If(y, z) Property MyInt As Integer Dim x = From n In numbers Where n > 0 Order By n Dim srcTree As XDocument = <?xml version="1.0" encoding="utf-8" standalone="yes"?> <!--This is a comment--> <Root> <Child1>data1</Child1> <Child2>data2</Child2> <Info3>info3</Info3> <Info4>info4</Info4> </Root> Dim doc As XDocument = <?xml version="1.0" encoding="utf-8" standalone="yes"?> <!--This is a comment--> <Root> <%= From el In srcTree.<Root>.Elements Where CStr(el).StartsWith("data") Select el %> </Root> Console.WriteLine(doc)

XDocument srcTree = new XDocument( new XComment("This is a comment"), new XElement("Root", new XElement("Child1", "data1"), new XElement("Child2", "data2"), new XElement("Info3", "info3"), new XElement("Info4", "info4") ) ); XDocument doc = new XDocument( new XComment("This is a comment"), new XElement("Root", from el in srcTree.Element("Root").Elements() where ((string)el).StartsWith("data") select el ) ); Console.WriteLine(doc);

[edit] Adoption and community support


Both C# and VB.NET have high adoption rates, and very active developer communities and Microsoft fully supports both communities. Most .NET Framework developers use only VB.NET.[8] However, C# does have an advantage in terms of the level of community activity on the Internet and there are more books available for C#. Examples of community and industry adoption include:

A 2007 Forrester Research poll revealed that 59% of .NET developers used only VB.NET to create software.[8] Visual Basic Express is the most popular download of all the Visual Studio Express downloads.[9] An original C# language designer, Scott Wiltamuth, stated in a March 2010 blog that the "most reliable numbers we have... show roughly equal adoption" for VB.NET and C#.[10] According to a survey conducted by Visual Studio Magazine "41 percent said they used C#, 34 percent programmed in VB.NET, while 25 percent responded with 'other.'"[9] Stephen Wiley, marketing product manager at Apress has reported "C# titles outsell VB.NET title books handily, by somewhere between a 21 and 31 margin."[9] MSDN Blogs, the blogging site for Microsoft employees, has 27,500 posts that discuss C#, while only 8,880 mention VB.Net (as of November 15, 2007) Google Groups, a Usenet search engine, returns 36,900 hits for "VB .Net", and 65,700 for C# Amazon.com returns 9,923 hits for C#, and 2,149 hits for "Visual Basic .Net" (as of November 15, 2007). Telerik Survey 2008 suggested that C# (63%) had surpassed VB.NET (34%) as the primary programming language.[11] Telerik Survey 2009 suggested that C# (69%) further strengthens its dominance over VB.NET (30%) as the primary programming language.[11]

[edit] Other languages


[edit] C++/CLI (formerly Managed C++)
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, destructors, 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

[edit] 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 Team System supports automatic generation of Unit Tests in C#, VB.Net, and C++, but excludes J#. J# has been discontinued and is not included in Visual Studio 2008 or newer versions, because Sun Microsystems sued Microsoft for ripping off their language, java.

[edit] Additional .NET languages


Main article: List of CLI languages All .NET languages compile down to Common Intermediate Language (CIL), which contains rich metadata and is functionally and logically equivalent to the original .NET language code. For these reasons, while it is possible to code directly in CIL, it is rarely done. The equivalency of CIL to .NET language code permits tools such as .NET Reflector to transform a .NET assembly into source code that is nearly identical to the original source. Code obfuscators are often used to guard against this, and operate by directly modifying the CIL of an assembly in order to make it difficult or impossible to de-compile to a higher level .NET language.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy