Understanding Operators
Operators assist you in manipulating data by assigning variable values, retrieving variable values, or comparing variable values. An operator falls into one of the following categories: arithmetic, assignment, bitwise, comparison, concatenation, or logical.Each time you declare a variable, you're assigning the variable to a value, so you're using an assignment operator. When you compare the value of one variable to the value of another variable, you're using a comparison operator. When you add numbers together, you're using an arithmetic operator. You used operators often last week, and most operators are self-explanatory in what purpose they actually have. If you're coming from a Visual Basic 6 background, you'll be pleasantly surprised by some of the new operators available to you in Visual Basic .NET, so make sure that you read through the following section.
Arithmetic Operators
In grade school, we all learned how to add, subtract, multiply, and divide. These are basic life functions, just like watching Star Trek and eating pizza. Visual Basic .NET offers the arithmetic operators listed in Table 8.2 that handle the dirty work of doing math.
Visual Basic .NET | C# | Description |
---|---|---|
+ | + | Addition |
- | - | Subtraction |
* | * | Multiplication |
/ | / | Division |
\ | N/A | Integer division |
Mod | % | Modulo |
^ | N/A | Exponentiation |
N/A | ++ | Unary addition |
N/A | -- | Unary subtraction |
Listing 8.3 Using Arithmetic Operators to Test Values

Private Sub testOperators()
Dim X As Integer = 50
Dim Y As Integer = 10
' Addition - returns 60
MessageBox.Show((X + Y).ToString())
' Subtraction - returns 40
MessageBox.Show((X - Y).ToString())
' Multiplication - returns 500
MessageBox.Show((X * Y).ToString())
' Division - returns 5
MessageBox.Show((X / Y).ToString())
' Modulo - returns 0
MessageBox.Show((X Mod Y).ToString())
' Modulo - returns 10
MessageBox.Show((Y Mod X).ToString())
' Exponent - returns 9.76562E+16
MessageBox.Show((X ^ Y).ToString())
End Sub

private void testOperators()
{
int X = 50;
int Y = 10;
// Addition - returns 60
MessageBox.Show((X + Y).ToString());
// Subtraction - returns 40
MessageBox.Show((X - Y).ToString());
// Multiplication - returns 500
MessageBox.Show((X * Y).ToString());
// Division - returns 5
MessageBox.Show((X / Y).ToString());
// Modulo - returns 0
MessageBox.Show((X % Y).ToString());
}
Assignment Operators
Assignment operators take a value from the right side of the operator and assign it to the value on the left side of the operator. In previous Visual Basic versions, the = sign was the assignment operator, but the language has been enhanced to include some pretty cool new assignment operators. Table 8.3 lists the assignment operators in Visual Basic .NET and C# and their descriptions.
Listing 8.4 Using Assignment Operators to Test Values

Private Sub testOperators()
Dim X As Integer = 50
Dim Y As Integer = 10
Dim z As Integer
' Addition - returns 60
X += Y
MessageBox.Show(X)
' Subtraction - returns 50
X -= Y
MessageBox.Show(X)
' Multiplication - returns 500
X *= Y
MessageBox.Show(X)
' Division - returns 50
X /= Y
MessageBox.Show(X)
' Assignment - returns FALSE
MessageBox.Show((X = Y).ToString())
End Sub

You'll notice that the Visual Basic .NET code is slightly different from the C# code in the way the operator is handled. In Visual Basic .NET, you must explicitly use the right side of the operator in an expression, so placing all the code in the single MessageBox statement doesn't work.
private void testOperators()
{
int X = 50;
int Y = 10;
// Addition - returns 60
MessageBox.Show((X += Y).ToString());
// Subtraction - returns 50
MessageBox.Show((X -= Y).ToString());
// Multiplication - returns 500
MessageBox.Show((X *= Y).ToString());
// Division - returns 50
MessageBox.Show((X /= Y).ToString());
// Assignment - returns 10
MessageBox.Show((X = Y).ToString());
}
Comparison Operators
Comparison operators evaluate the expression on the right side of the equal sign and return a Boolean true or false based on the comparison of the expressions. Comparison operators also can be grouped in the relational and equality operators group. In all cases, a Boolean true or false is returned to the expression you're attempting to evaluate. Table 8.4 lists the comparison operators in Visual Basic .NET and C# and their descriptions.
Listing 8.5 Using Comparison Operators to Test Variable Values

Private Sub testOperators()
Dim X As Integer = 50
Dim Y As Integer = 10
If X < Y Then
' This never shows, X is NOT less then Y
MessageBox.Show("X greater than Y")
End If
Dim s1 As String = "Bob"
Dim s2 As String = "bob"
If s1 = s2 Then
' This never shows, Bob does not equal bob
MessageBox.Show("s1 = s2")
End If
If s1 = "Bob" OrElse s2 = "Bob" Then
' This shows, s1 or s2 = Bob
MessageBox.Show("s1 or s2 = Bob")
End If
End Sub

You can see that when dealing with strings, checking the equality in C# uses the == operator, whereas Visual Basic .NET uses the = operator.
private void testOperators()
{
int X = 50;
int Y = 10;
if (X < Y)
{
// This never shows, X is NOT less then Y
MessageBox.Show("X greater than Y");
}
string s1 = "Bob";
string s2 = "bob";
if (s1 == s2)
{
// This never shows, Bob does not equal bob
MessageBox.Show("s1 = s2");
}
if (s1 == "Bob" || s2 == "Bob")
{
// This shows, s1 or s2 = Bob
MessageBox.Show("s1 or s2 = Bob");
}
}