Using Operators in C# Programming
 
 

An action can be an assignment, a comparison, or a mathematical calculation. C# has several operators that we'll discuss in this article to provide you with the ability to perform calculations and actions on you variables.

Arithmetic Operators

Arithmetic operators are probably the most common in a C# program next to the simple assignment operator. Let's do a quick review of the assignment operator from the last chapter.

int mynumber = 1;

string mystring = "Hello";

The assignment operator is the equal sign. This operator simply tells the compiler to assign the value on the right side to the variable on the left side. If you recall, the variable is a friendly way to represent a block of memory space. It then stores the value in memory.

After you assign a value to a variable, you want to manipulate that value in some way. You wouldn't do arithmetic operations on a string variable, but you might want to use arithmetic on the integer variable named mynumber.

The following are the main arithmetic variables.

  • Addition +

  • Subtraction –

  • Multiplication *

  • Division /

  • Modulus %

  •  Increment ++

  • Decrement –

You probably recognize the first four, but the last three are a bit more complicated. Let's first take a look at the first four, and then we'll explain the last three.

The first operator is the addition operator. This operator adds two values. The values can be static or values you store in another variable. Let's take a look at an example.

int mynumber1 = 3;

int mynumber2 = 4;

int mynumber_total = mynumber1 + mynumber2;

In this example, we assigned two variables with static numeric values 3 and 4. We then add those two variables and store the result in a third variable named mynumber_total. This variable holds the value 7. Remember that an integer takes 2 bytes in memory, so you know that this program needs at least 2 bytes of memory.

Next is the subtraction operatory. This operator is just as simple as the addition operator. Let's use the previous code to see an example of the subtraction operator.

int mynumber1 = 3;

int mynumber2 = 4;

int mynumber_total = mynumber1 - mynumber2;

Notice that the only change we made from the previous code is to change the addition sign to a subtraction sign. The result is subtraction of mynumber2 from mynumber1. The result is -1. You can also store negative integer numbers in an integer variable.

Multiplication is slightly less intuitive if you're not used to computer calculations. In any computer language, the asterisks symbol is used to represent multiplication instead of the "x" notation you're used to in your studies. Let's take a look at a sample multiplication calculation.

int mynumber1 = 3;

int mynumber2 = 4;

int mynumber_total = mynumber1 * mynumber2;

Again, we changed only one part of this calculation. We only changed the multiplication symbol. The result is 12 and stored in the mynumber_total variable.

The final common operator is the division operator. Up until now, we've used integers to store the result of our calculation. One issue with division is that you can often result with a decimal number. In the previous examples, we used an integer for the result variable, but this would be an issue of the result is a decimal. To ensure that we don't introduce a bug in our code, we'll change the mynumber_total variable to a float data type.

int mynumber1 = 3;

int mynumber2 = 4;

float mynumber_total = mynumber1 / mynumber2;

We made two changes this time to our code statements. We changed the calculation to the division operator, but we also changed the variable data types to float to account for any decimal values. In this case, it's beneficial to us and we avoid a bug. The result of the calculation is .75. If we left the value as an integer, the compiler either throws an error or truncates the value to 0.

The previous samples are a part of the common and well-known operators, but we have three more arithmetic operators that aren't as simple to understand. The first one is the modulus operator. This operator is similar to the division operator, but it retrieves the remainder of a calculation. This operator is used when you want to detect even numbers. For instance, suppose you want to know if a variable is divisible by 5. For instance, 25 is divisible by 5. We know this because no remainder is given if we perform the calculation. However, if we devide 24 by 5, we get a remainder of 4. Because of this remainder, we know that 24 is not divisible by 5, and we can detect it using the modulus operator.

Let's take a look at example statements.

int mynumber1 = 24;

int mynumber2 = 5;

float mynumber_total = mynumber1 % mynumber2;

In the above example, we used the same 25 and 4 logic. The modulus operator divides the two numbers and sends the remainder to the mynumber_total variable.

The next operator is the increment operator. The increment operator adds 1 to a value. It's shorthand for addition operations. Let's first take a look at a simple addition operation that adds 1 to a number.

int mynumber1 = 5;

mynumber = mynumber + 1;

As you can probably guess, the above calculation results in mynumber containing the value of 6. Instead of writing a full statement to add 1 to a number, you can use the increment operator. Let's use the operator to write a shorthand statement.

int mynumber1 = 5;

mynumber++;

Now we have much cleaner, shorter code. It's easier to read, and it performs the same action as the previous statement.

The increment operator can be set as postfix or prefix. This concept can be a bit confusing for new programmers. Let's take a look at both.

Postfix:           mynumber++

Prefix:                         ++mynumber

It's important to note the postfix and prefix notation, because the way you write your calculation can change the expected output. Let's first take a look at the postfix notation.

int mynumber1 = 5;

int result = mynumber++;

The postfix increment operator tells the compiler "first assign the value of mynumber to result and then increment mynumber by 1." In other words, first the value of mynumber is assigned to result, and then mynumber is incremented.

The above result variable equals 5 and mynumber equals 6. We first assign 5 to result and then increment mynumber to 6.

Now let's look at a prefix increment operator.

int mynumber1 = 5;

int result = ++mynumber;

The prefix increment operator has different logic than the postfix version. The above code tells the compiler "add 1 to mynumber and then assign it to result. In the above example, mynumber is incremented to 6 and then 6 is assigned to result.

Let's take a look at both of these operations side-by-side.

int mynumber1 = 5;

int result = ++mynumber; // increments mynumber to 6 and assigns 6 to result

// At this point, result equals 6 and mynumber equals 6.

int result2 = mynumber++; // assign 6 to result2 and then increment mynumber

// Now result2 equals 6 and mynumber equals 7.

The difference is small, but you can see that using the wrong operator could cause an incorrect result in your code. The result would be a bug in your code.

The decrement operator works in the same way except it subtracts one from the variable. Let's take a look at the inefficient way to subtract 1 from your variable.

int mynumber1 = 5;

mynumber1 = mynumber - 1;

The above code is accurate and proper syntax. However, you can use shorthand with the decrement operator to subtract one from the variable. Let's make this code more efficient and easily read.

int mynumber1 = 5;

mynumber--;

The above code does the same as the previous section. It subtracts one from mynumber. The previous code is correct, but the above code is much more efficient. Most programmers read through their code very quickly, and this notation makes the code easier to read for even a secondary person who is responsible for maintaining your code.

Just like the increment operator, the decrement operator also has a prefix and a postfix notation. The notation you use will affect the outcome of your calculation, so it's important to understand the difference.

The postfix and prefix decrement operators are the following.

Postfix: mynumber--

Prefix: --mynumber

First let's take a look at the postfix decrement operator.

int mynumber1 = 5;

int result = mynumber--;

This calculation tells the compiler "assign the value of mynumber to result, and then subtract 1 from mynumber." In this example, 5 is assigned to result, and then mynumber is decremented to 4.

Now let's look at the decrement operator in prefix notation.

int mynumber1 = 5;

int result = --mynumber;

The above code tells the compiler "decrement mynumber by 1 and then assign the value to the result variable." In this example, mynumber is decremented to 4, and the value is assigned to result. In the above code, mynumber is equal to 4, and then this value is assigned to result.

Let's combine both the postfix and prefix sample code and look at how the decrement operators work. We'll use the same code that we used in the increment operation.

int mynumber1 = 5;

int result = --mynumber; // decrements mynumber to 4 and assigns 4 to result

// At this point, result equals 4 and mynumber equals 4.

int result2 = mynumber--; // assign 4 to result2 and then decrement mynumber by 1

// Now result2 equals 4 and mynumber equals 3.

Again, you'll notice from the above code that the order in which you perform the assignment and decrement operation determines the outcome of your calculation. This is important when you code your applications, or you can introduce a bug into the application.

Now let's cascade these four operations together to see the different results we get when we use postfix and prefix. This will help you understand the difference in results.

int mynumber1 = 5;

int result = ++mynumber; // increments mynumber to 6 and assigns 6 to result

// At this point, result equals 6 and mynumber equals 6.

int result2 = mynumber++; // assign 6 to result2 and then increment mynumber

// Now result2 equals 6 and mynumber equals 7.

//Now let's use the decrement operator on the mynumber variable.

int result3 = --mynumber; // decrements mynumber to 6 and assigns 6 to result3

// At this point, result3 equals 6 and mynumber equals 6.

int result4 = mynumber--; // assign 6 to result4 and then decrement mynumber by 1

// Now result4 equals 6 and mynumber equals 5.

It takes some time to understand the postfix and prefix operations. Take some time to decrement and increment with the different operators in Visual Studio. Step through the program to review the way operations change values in the different variables.

Assignment Operators

Assignment operators also work in shorthand form. They allow you to add, subtract, multiply, divide and even use the modulus operation in a shorter statement than a standard calculation.

The following is a list of the common assignment operators.

  • Equals: =

  • Add and assign: +=

  • Subtract and assign: -=

  • Multiply and assign: *=

  • Divide and assign: /=

  • Modulus and assign: %=

We already mentioned the equals assignment operator in the first part of the chapter. This assignment operator just assigns a value to a variable.

The add and assign operator adds a value to the current variable and then assigns the result to the variable. Let's take a look at an example.

int mynumber1 = 5;

mynumber += 5;

What this statement says is "add 5 to mynumber and then assign the result to mynumber." The result is that mynumber now equals 10. The above statement is the same as using the following code.

int mynumber1 = 5;

mynumber = mynumber + 5;

The first code was shorthand and much easier to read. It's shorter, and even more efficient for the compiler.

You can do the same with the subtraction assignment operator. Let's take a look at the code.

int mynumber1 = 5;

mynumber -= 5;

The above code subtracts five from mynumber and then assigns the result to the mynumber variable. The result is 0. The above operation is the same as the following.

int mynumber1 = 5;

mynumber = mynumber - 5;

You can probably see a pattern, but we still have two more assignment operators. The next assignment operator is the multiplication operator. Let's take a look at some sample code.

int mynumber1 = 5;

mynumber *= 5;

The above code tells the compiler "multiply mynumber by 5 and then assign the result to the mynumber variable." The result is that mynumber equals 25. The above code is the same as using the following notation.

int mynumber1 = 5;

mynumber = mynumber * 5;

C# also has the division assignment operator. The following is an example of this operator.

int mynumber1 = 5;

mynumber /= 5;

The above code tells the compiler to divide mynumber by 5 and then assign the value to mynumber again. In this calculation, the result is that mynumber equals 1. The above code is the same as the following notation.

int mynumber1 = 5;

mynumber = mynumber / 5;

The final assignment operator is the modulus assignment. Remember that the modulus operator divides one value by another, but it returns the remainder. Let's take a look at the modulus assignment operator.

int mynumber1 = 5;

mynumber %= 5;

In this example, the compiler is told to divide 5 by mynumber and return the result. Since there is no remainder in this calculation, the result is that mynumber contains the value 0. The above code is the same as the following notation.

int mynumber1 = 5;

mynumber = mynumber % 5;

These assignment operators make your coding life easier by saving you a few keystrokes, and they make your code easier to read. You should use them when you're able to substitute shorthand for the longer statement.

With some practice, you'll get used to the shorthand notation in these operators. It's important to learn how each of these statements work, because you'll see them in every program whether it's your own or you've been assigned to maintain someone else's code.