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.
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#.
psStr = psstr & "Hello" plVAL1 = plval1 + plval2 |
psStr = psStr & "Hello" plVal1 = plVal1 + plVal2 |
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#.
n += 10 MyFunction("Brian", "Jon") MyLongerFunction("Brian", _ "Jon") |
n += 10; MyFunction("Brian", "Jon") MyLongerFunction("Brian", "Jon"); |
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.
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.
MyArray[0] = "Brian"; MyArray[1] = "Jon"; MyCollection["Brian"] = "Cool"; MyCollection["Jon"] = "Not So Cool"; |
Functions are declared very differently in C# than in Visual Basic. Listing A.26 illustrates a typical function implementation in Visual Basic.
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#.
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.
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#.
In Visual Basic, the standard If statement takes the form of the code in Listing A.28.
If plVal < 10 Then ... Else ... End If |
In C#, this format is somewhat modified. Listing A.29 shows the same 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.
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.
For plCount = 0 to 100 plVal = plVal + 1 Next |
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.
for(initialization; test; increment) { ... statements; ... } |
The while loop is very similar in both programming languages. Listing A.33 shows a typical while loop in Visual Basic.
While plVal1 < plVal2 plVal1 = plVal1 + 1 Loop |
In the C# programming language, this same loop would be written as shown in Listing A .34.
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.
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.
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.
switch(plVal) { case 1: psStr = "Brian"; break; case 2: psStr = "Jon"; break; default: psStr = "Default!"; break; } |