Pages

Men

rh

11/12/2012

Difference between VB.NET and C#


Features of Visual Basic .NET not found in C#
  • Variables can be declared using the With Events 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
  • 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)
  •  
  • Inline date declarations by using #1/1/2000# syntax (M/dd/yyyy).
  • Module (although C#'s sealed static classes with additional semantics, but each field has to individually be declared as static)
  • Members of Modules imported to the current file, can be access with no preceding container accessor
  • The My namespace
  • COM components and interoperability was more powerful in VB.NET as the Object type is bound at runtime
  • Namespaces can be imported in project level, so they don't have to be imported to each individual file, like C#
  • In-line exceptions filtering by a Boolean expression, using "When expression" blocks.C# emulates this functionality,using a catch block followed by an if block. It is not just syntax sugar.
  • With instruction Executes a series of instructions repeatedly refer to a single object or structure. (similar to the third bullet point above)
 Features of C# not found in Visual Basic .NET
  • Allows blocks of unsafe code (like C++/CLI) via the unsafe keyword
  • Partial Interfaces
  • 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 
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.
  • When assigning a value to a variable with a different data type, VB.NET will coerec the value if possible. This automatic coercion can sometimes lead to unexpected results, for example:

Dim i As Integer = "1" 'Compiler automatically converts String to Integer
Dim j As String = 1 'Compiler automatically converts Integer to String
If i = j Then 'Compiler does cast and compare between i and j
    MessageBox.Show("Avoid using, but this message will appear!")
End If

It should be noted that although the default is for 'Option Strict' is off, it is recommended by Microsoft[15] and widely considered to be a good practice to turn 'Option Strict' "on", due to the fact it increases application performance, and eliminates the chance of naming errors and other programming mistakes.[16]
  • Val() function which also parses a 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 a null value)
  • CInt, CStr, CByte, CDbl, CBool, CByte, CDate, CLng, CCur, CObj and a wide variety of converting functions built in the language
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)
  • String concatenation can be performed using the numeric addition token, +, in addition to the string concatenation token &. Although these tokens are syntactically interchangeable, use of the addition token should be confined to mathematical operations.
  • In Visual Basic .NET property methods may take parameters.
  • C# is case-sensitive.
 Keywords
Visual Basic is not case sensitive, which means any combinations of upper and lower cases in keywords are acceptable. However Visual Studio automatically converts all Visual Basic keywords to the default capitalised forms, e.g. "Public", "If".
C# is case sensitive and all C# keywords are in lower cases.
Visual Basic and C# share most keywords, with the difference being the default (Remember Visual Basic is not case sensitive) Visual Basic keywords are the capitalised versions of the C# keywords, e.g. "Public" vs "public", "If" vs "if".

A few keywords have very different versions in Visual Basic and C#:
  • Friend vs internal - access modifiers allowing inter-class but not intra-assembly reference
  • Me vs this - a self-reference to the current object instance
  • 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
  • MyBase vs base - for referring to the base class from which the current class is derived
  • 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
  • Overridable vs virtual - declares a method as being able to be overridden in derived classes
  • Shared vs static - for declaring methods that do not require an explicit instance of an object
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.