Sunday, April 27, 2008

Differences Between VB.NET& C# .NET-3

Programming Difference
Purpose VB.NET C#

Declaring Variables
VB.NET>>Dim x As Integer
Public x As Integer = 10
C#.NET>>int x;
int x = 10;

Comments
VB.NET>> ' comment
x = 1 ' comment
Rem comment

C#.NET>>// comment
/* multiline
comment */

Assignment Statements
nVal = 7
nVal = 7;

Conditional Statements

VB.NET>>
If nCnt <= nMax Then
' Same as nTotal =
' nTotal + nCnt.
nTotal += nCnt
' Same as nCnt = nCnt + 1.
nCnt += 1
Else
nTotal += nCnt
nCnt -= 1
End If

C#.NET>>
if (nCnt <= nMax)
{
nTotal += nCnt;
nCnt++;
}
else
{
nTotal +=nCnt;
nCnt--;
}



Selection Statements

VB.NET>>
Select Case n
Case 0
MsgBox ("Zero")
' Visual Basic .NET exits
' the Select at
' the end of a Case.
Case 1
MsgBox ("One")
Case 2
MsgBox ("Two")
Case Else
MsgBox ("Default")
End Select


C#.NET>>

switch(n)
{
case 0:
Console.WriteLine("Zero");
break;
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
break;
default:
Console.WriteLine("?");
break;
}

FOR Loops

VB.NET>>

For n = 1 To 10
MsgBox("The number is " & n)
Next

For Each prop In obj
prop = 42
Next prop

C#.NET>>
for (int i = 1; i <= 10; i++)
Console.WriteLine(
"The number is {0}", i);
foreach(prop current in obj)
{
current=42;
}

Hiding Base Class Members

VB.NET>>
Public Class BaseCls
' The element to be shadowed
Public Z As Integer = 100
public Sub Test()
System.Console.WriteLine( _
"Test in BaseCls")
End Sub
End Class

Public Class DervCls
Inherits BaseCls
' The shadowing element.
Public Shadows Z As String = "*"
public Shadows Sub Test()
System.Console.WriteLine( _
"Test in DervCls")
End Sub
End Class

Public Class UseClasses
' DervCls widens to BaseCls.
Dim BObj As BaseCls =
New DervCls()
' Access through derived
' class.
Dim DObj As DervCls =
New DervCls()

Public Sub ShowZ()
System.Console.WriteLine( _
"Accessed through base "&_
"class: " & BObj.Z)
System.Console.WriteLine(_
"Accessed through derived "&_
"class: " & DObj.Z)
BObj.Test()
DObj.Test()
End Sub
End Class


C#.NET>>
public class BaseCls
{
// The element to be hidden
public int Z = 100;
public void Test()
{
System.Console.WriteLine(
"Test in BaseCls");
}
}

public class DervCls : BaseCls
{
// The hiding element
public new string Z = "*";
public new void Test()
{
System.Console.WriteLine(
"Test in DervCls");
}
}

public class UseClasses
{
// DervCls widens to BaseCls
BaseCls BObj = new DervCls();
// Access through derived
//class
DervCls DObj = new DervCls();
public void ShowZ()
{
System.Console.WriteLine(
"Accessed through " +
"base class: {0}",
BObj.Z);
System.Console.WriteLine(
"Accessed through" +
" derived class:{0}",
DObj.Z);
BObj.Test();
DObj.Test();
}
}
WHILE Loops

VB.NET>>
' Test at start of loop
While n < 100 .
' Same as n = n + 1.
n += 1
End While '

C#.NET>>
while (n < 100)
n++;



Parameter Passing by Value
VB.NET>>
' The argument Y is
'passed by value.
Public Sub ABC( _
ByVal y As Long)
'If ABC changes y, the
' changes do not affect x.
End Sub

ABC(x) ' Call the procedure.
' You can force parameters to
' be passed by value,
' regardless of how
' they are declared,
' by enclosing
' the parameters in
' extra parentheses.
ABC((x))

C#.NET>>
/* Note that there is
no way to pass reference
types (objects) strictly
by value. You can choose
to either pass the reference
(essentially a pointer), or
a reference to the reference
(a pointer to a pointer).*/
// The method:
void ABC(int x)
{
...
}
// Calling the method:
ABC(i);
Parameter Passing by Reference

VB.NET>>
Public Sub ABC(ByRef y As Long)
' The parameter y is declared
'by referece:
' If ABC changes y, the changes are
' made to the value of x.
End Sub

ABC(x) ' Call the procedure.




C#.NET>>
/* Note that there is no
way to pass reference types
(objects) strictly by value.
You can choose to either
pass the reference
(essentially a pointer),
or a reference to the
reference (a pointer to a
pointer).*/
// Note also that unsafe C#
//methods can take pointers
//just like C++ methods. For
//details, see unsafe.
// The method:
void ABC(ref int x)
{
...
}
// Calling the method:
ABC(ref i);

Structured Exception Handling

VB.NET>>
Try
If x = 0 Then
Throw New Exception( _
"x equals zero")
Else
Throw New Exception( _
"x does not equal zero")
End If
Catch err As System.Exception
MsgBox( _
"Error: " & Err.Description)
Finally
MsgBox( _
"Executing finally block.")
End Try


C#.NET>>
// try-catch-finally
try
{
if (x == 0)
throw new System.Exception(
"x equals zero");
else
throw new System.Exception(
"x does not equal zero");
}
catch (System.Exception err)
{
System.Console.WriteLine(
err.Message);
}
finally
{
System.Console.WriteLine(
"executing finally block");
}



Set an Object Reference to Nothing

VB.NET>> o = Nothing
C#.NET>> o = null;

Initializing Value Types

VB.NET>>
Dim dt as New System.DateTime( _
2001, 4, 12, 22, 16, 49, 844)

C#.NET>>
System.DateTime dt =
new System.DateTime(
2001, 4, 12, 22, 16,
49, 844);



New Features of both languages in 2005 version



VB.NET>>>>>
Visual Basic 2005 has many new and improved language features -- such as inheritance, interfaces, overriding, shared members, and overloading -- that make it a powerful object-oriented programming language. As a Visual Basic developer, you can now create multithreaded, scalable applications using explicit multithreading. This language has following new features,
1. Continue Statement, which immediately skips to the next iteration of a Do, For, or While loop.
2. IsNot operator, which you can avoid using the Not and Is operators in an awkward order.
3. 3. Using...End. Using statement block ensures disposal of a system resource when your code leaves the block for any reason.
4. Public Sub setbigbold( _
5. ByVal c As Control)
6. Using nf As New _
7. System.Drawing.Font("Arial",_
8. 12.0F, FontStyle.Bold)
9. c.Font = nf
10. c.Text = "This is" &_
11. "12-point Arial bold"
12. End Using
End Sub
13. Explicit Zero Lower Bound on an Array, Visual Basic now permits an array declaration to specify the lower bound (0) of each dimension along with the upper bound.
14. Unsigned Types, Visual Basic now supports unsigned integer data types (UShort, UInteger, and ULong) as well as the signed type SByte.
15. Operator Overloading, Visual Basic now allows you to define a standard operator (such as +, &, Not, or Mod) on a class or structure you have defined.
16. Partial Types, to separate generated code from your authored code into separate source files.
17. Visual Basic now supports type parameters on generic classes, structures, interfaces, procedures, and delegates. A corresponding type argument specifies at compilation time the data type of one of the elements in the generic type.
18. Custom Events. You can declare custom events by using the Custom keyword as a modifier for the Event statement. In a custom event, you specify exactly what happens when code adds or removes an event handler to or from the event, or when code raises the event.
19. Compiler Checking Options, The /nowarn and /warnaserror options provide more control over how warnings are handled. Each one of these compiler options now takes a list of warning IDs as an optional parameter, to specify to which warnings the option applies.
20. There are eight new command-line compiler options:
a. The /codepage option specifies which codepage to use when opening source files.
b. The /doc option generates an XML documentation file based on comments within your code.
c. The /errorreport option provides a convenient way to report a Visual Basic internal compiler error to Microsoft.
d. The /filealign option specifies the size of sections in your output file.
e. The /noconfig option causes the compiler to ignore the Vbc.rsp file.
f. The /nostdlib option prevents the import of mscorlib.dll, which defines the entire System namespace.
g. The /platform option specifies the processor to be targeted by the output file, in those situations where it is necessary to explicitly specify it.
h. The /unify option suppresses warnings resulting from a mismatch between the versions of directly and indirectly referenced assemblies.






C#.NET>>>>>>>

With the release of Visual Studio 2005, the C# language has been updated to version 2.0. This language has following new features:
1. Generics types are added to the language to enable programmers to achieve a high level of code reuse and enhanced performance for collection classes. Generic types can differ only by arity. Parameters can also be forced to be specific types.
2. Iterators make it easier to dictate how a for each loop will iterate over a collection's contents.
3. // Iterator Example
4. public class NumChar
5. {
6. string[] saNum = {
7. "One", "Two", "Three",
8. "Four", "Five", "Six",
9. "Seven", "Eight", "Nine",
10. "Zero"};
11. public
12. System.Collections.IEnumerator
13. GetEnumerator()
14. {
15. foreach (string num in saNum)
16. yield return num;
17. }
18. }
19. // Create an instance of
20. // the collection class
21. NumChar oNumChar = new NumChar();
22. // Iterate through it with foreach
23. foreach (string num in oNumChar)
24. Console.WriteLine(num);
25. Partial type definitions allow a single type, such as a class, to be split into multiple files. The Visual Studio designer uses this feature to separate its generated code from user code.
26. Nullable types allow a variable to contain a value that is undefined.
27. Anonymous Method is now possible to pass a block of code as a parameter. Anywhere a delegate is expected, a code block can be used instead: There is no need to define a new method.
28. button1.Click +=
29. delegate { MessageBox.Show(
"Click!") };
30. . The namespace alias qualifier (::) provides more control over accessing namespace members. The global :: alias allows to access the root namespace that may be hidden by an entity in your code.
31. Static classes are a safe and convenient way of declaring a class containing static methods that cannot be instantiated. In C# v1.2 you would have defined the class constructor as private to prevent the class being instantiated.
32. 8. There are eight new compiler options:
a. /langversion option: Can be used to specify compatibility with a specific version of the language.
b. /platform option: Enables you to target IPF (IA64 or Itanium) and AMD64 architectures.
c. #pragma warning: Used to disable and enable individual warnings in code.
d. /linkresource option: Contains additional options.
e. /errorreport option: Can be used to report internal compiler errors to Microsoft over the Internet.
f. /keycontainer and /keyfile: Support specifying cryptographic keys.

Conclusion
I think that this article will help you understand both language features and also you can choose a language based on your taste. I will update this article in future if there are any changes.


<<<<PREVIOUS

No comments: