Microsoft 9GD00001 Computer Accessories User Manual


 
54 Microsoft Visual Studio 2010: A Beginner’s Guide
Consistent with Table 2-2, C# uses int and VB uses Integer as their native type
definitions for a 32-bit signed integer. Additionally, you see age defined in both C# and
VB using the .NET type, Int32. Notice that the .NET type is the same in both languages.
In fact, the .NET type will always be the same for every language that runs in .NET. Each
language has its own syntax for the .NET types, and each of the language-specific types is
said to alias the .NET type.
Expressions
When performing computations in your code, you’ll do so through expressions, which are
a combination of variables, operators (such as addition or multiplication), or referencing
other class members. Here’s an expression that performs a mathematical calculation and
assigns the result to an integer variable:
C#:
int result = 3 + 5 * 7;
VB:
Dim result As Int32 = 3 + 5 * 7
A variable that was named result in this example is a C# type int or a VB type Int32,
as specified in Table 2-2. The variable could be named pretty much anything you want;
I chose the word result for this example. The type of our new variable result in the VB
example is Int32, which is a primitive .NET type. You could have used the VB keyword
Integer, which is an alias for Int32 instead. The expression is 3 + 5 * 7, which contains
the operators + (addition) and * (multiplication) and is calculated and assigned to result
when the program runs. The value of result will be 38 because expressions use standard
algebraic precedence. In the preceding example, 5 * 7 is calculated first, multiplication
has precedence, and that result is added to 3.
You can modify the order of operations with parentheses. Here’s an example that adds
3 to 5 and then multiplies by 7:
C#:
int differentResult = (3 + 5) * 7;
VB:
Dim differentResult As Int32 = (3 + 5) * 7
Because of the grouping with parentheses, differentResult will have the value 56 after
this statement executes.