
C# DEVELOPMENT
Converting from Visual Basic .NET to C#
Clive Chinery 04.21.2004
Rating: -4.60- (out of 5)




This tip provides tips you can use when converting code from VB .NET to C#, if you learned the languages in that order.
Assuming you took Dan Appleman's advice and learned Visual Basic .NET before learning C#, here are the things I wish someone had told me instead of having to learn it the hard way.
- C# is case-sensitive. Thus although both "if" and "If" are valid in VB.NET, only "if" is valid in C#.
- Visual Basic .NET uses "=" for both equality and assignment whereas C# uses "==" for equality and "=" for assignment.
- C# uses the ";" to denote the end of a statement and thus does not need a line continuation character.
- C# uses braces, "{" and "}", to denote blocks of code.
- In C#, everything after "//" is a comment. Multiple lines can be commented out with "/* ..... */".
- C# distinguishes between a char (a single byte) and a string. The literals for each are shown here:
char cFred = 'F'; // character assignment
string strFred = "Fred"; // string assignment
- Brackets around conditions are mandatory in C#, thus the VB.NET fragment of code:
If intFred = 1 Then
strFred = "Fred is 1"
End If
becomes in C#
if (intFred == 1)
{
strFred = "Fred is 1";
}
- The logical "And" and "Or" in Visual Basic .NET become just "&" and "|", thus:
If (intFred = 1) Or (intFred = 2) Then
strFred = "Fred is 1 or 2";
End If
becomes in C#:
if ((intFred == 1) | (intFred == 2)) // notice the second layer of (..)
{
strFred = "Fred is 1 or 2";
}
- Whereas in Visual Basic .NET, strings can be concatenated by "+" or "&", in C#, concatenation is "+" only.
The only C# book I have needed is C# in a Nutshell from O'Reilly (ISBN 0-596-00526-1).
Do you have comments on this tip? Let us know.
 |

|
Rate this Tip
|
To rate tips, you must be a member of SearchWinDevelopment.com. Register now
to start rating these tips. Log in if you are already a member.
|


');
// -->
DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.
|
 |
|
|
 |
|
 |