Opting for C#

If you are more familiar with the syntax of C, C++ or Java, you might be interested in using C# (pronounced “C sharp”) as your server-side programming language . If you are currently a Visual Basic programmer wanting to move to C#, you should be aware of a few syntactic changes. We will look at some of these differences in the next sections; however, be aware that this is not a comprehensive look at the entire C# programming language—it’s merely a guide to ease you into the C# language.

Case Sensitivity

One of the main differences between the languages is that C# is case-sensitive, while Visual Basic is not. Therefore, although the code in Listing A.20 is acceptable in Visual Basic, the code in Listing A.21 would be needed in C#.

Listing A.20. Valid Code (Visual Basic .NET)
psStr = psstr & "Hello" 
plVAL1 = plval1 + plval2 

Listing A.21. Valid Code (C#)
psStr = psStr & "Hello" 
plVal1 = plVal1 + plVal2 

Semicolons

One of the very first things to be aware of is the use of semicolons to end programming statements. In Visual Basic, statements are terminated by a carriage return; in C#, they are terminated with a ; character. Listing A.22 shows a few lines of code in Visual Basic, and Listing A.23 shows the equivalent code in C#.

Listing A.22. Some Statements (Visual Basic .NET)
n += 10 
MyFunction("Brian", "Jon") 
MyLongerFunction("Brian", _ 
    "Jon") 

Listing A.23. The Same Statements (C#)
n += 10; 
MyFunction("Brian", "Jon") 
MyLongerFunction("Brian", 
    "Jon"); 

Brackets

Another syntactical change is the type of brackets that are used for collection and array indexers. In Visual Basic, parentheses—( and ) —are used around these values; in C#, square brackets— [ and ] —are used. Listing A.24 shows the use of brackets in Visual Basic.

Listing A.24. Use of Brackets (Visual Basic .NET)
MyArray(0) = "Brian" 
MyArray(1) = "Jon" 

MyCollection("Brian") = "Cool" 
MyCollection("Jon") = "Not So Cool" 

In C#, these same statements would be written as shown in Listing A.25.

Listing A.25. Use of Brackets (C#)
MyArray[0] = "Brian"; 
MyArray[1] = "Jon"; 

MyCollection["Brian"] = "Cool"; 
MyCollection["Jon"] = "Not So Cool"; 

Functions

Functions are declared very differently in C# than in Visual Basic. Listing A.26 illustrates a typical function implementation in Visual Basic.

Listing A.26. Function (Visual Basic .NET)
Public Function MyFunction(psStr As String) As String 
    MyFunction = psStr 
End Function 

Now let’s look at that same function in C#. Listing A.27 is a listing of the same function in C#.

Listing A.27. Function (C#)
public String MyFunction(String psStr) 
{
    return psStr; 
} 

Let’s talk about the differences. First, note the case difference in each of the languages. It is extremely important that you remember that C# is a case-sensitive language. Second, you will notice that, in Visual Basic, the type of the function is noted at the end of the function line with the words As String. In C#, however, the type of the function is declared before the name of the function as String. Third, you will see that the Visual Basic function is closed with the End Function statement. In C#, all statements of a function are enclosed around curly braces—{ and }. Fourth, you’ll see that the parameters passed in the Visual Basic version are typed after the variable name, just like the function. In C#, the variables are typed before the variable name, again like the type of the function. Finally, the Visual Basic version of the function returns the value of psStr by assigning it to the name of the function, MyFunction. In C#, however, to return a value, you use the return statement, which does the same thing.

Control-of-Flow Statements

All the standard flow-control statements that you are familiar with in Visual Basic exist in C# and have the same functionality; however, their syntax is different. In this section, we look at all the flow-control statements and their syntax in C#.

If Statement

In Visual Basic, the standard If statement takes the form of the code in Listing A.28.

Listing A.28. If Statement (Visual Basic .NET)
If plVal < 10 Then 
    ... 
Else 
    ... 
End If 

In C#, this format is somewhat modified. Listing A.29 shows the same statement in C#.

Listing A.29. if Statement in (C#)
if (plVal < 10) 
{
    ... 
} 
else 
{
    ... 
} 

Although it isn’t drastically different, you should pay attention to the parentheses around the test statement and the use of curly braces to enclose the code statements. Also you will see that the End If clause is missing. Finally, note the case of the words if and else.

For Statement

The For loop is written very differently in C# than it is in Visual Basic. Listing A.30 shows a Visual Basic for loop, and Listing A.31 shows the same loop written in the C# programming language.

Listing A.30. For Statement (Visual Basic .NET)
For plCount = 0 to 100 
    plVal = plVal + 1 
Next 

Listing A.31. For Statement (C#)
for(plCount = 0; plCount < 100; plCount++) 
{
    plVal = plVal + 1; 
} 

Both of these loops perform the exact same function; however, they each have a very different syntax. Generically, the C# version of the for loop is written as shown in Listing A.32.

Listing A.32. Generic C#for Loop
for(initialization; test; increment) 
{
    ... 
    statements; 
    ... 
} 

While Loop

The while loop is very similar in both programming languages. Listing A.33 shows a typical while loop in Visual Basic.

Listing A.33. While Loop (Visual Basic .NET)
While plVal1 < plVal2 
    plVal1 = plVal1 + 1 
Loop 

In the C# programming language, this same loop would be written as shown in Listing A .34.

Listing A.34. While Loop (C#)
while(plVal1 < plVal2) 
{
    plVal1 = plVal1 + 1 
} 

This syntax is very similar to that of the for loop discussed previously. The two things to remember are the parentheses around the test statement and the curly braces in place of the Loop statement to enclose the statements to be executed while the conditional is true.

Switch Versus Select

In the world of Visual Basic, one of the most useful flow-control statements is the Select...Case..End Select statement. It looks like the code in Listing A .35.

Listing A.35. Select…Case…End Select Statement (Visual Basic .NET)
Select Case plVal 
    Case 1 
        psStr = "Brian" 
    Case 2 
        psStr = "Jon" 
    Case Else 
        psStr = "Else!" 
End Select 

In C#, this same statement is called a switch and is written like the code in Listing A.36.

Listing A.36. switch Statement (C#)
switch(plVal) 
{
    case 1: 
        psStr = "Brian"; 
        break; 
    case 2: 
        psStr = "Jon"; 
        break; 
    default: 
        psStr = "Default!"; 
        break; 
} 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset