c sharp

# C# Operators

As we all know, the operators are special symbols or signs on our keyboard used to perform different mathematical operators. These operations can be arithmetic, logical, comparison, or assignment according to the choice of operators we tend to apply on our operands, i.e., values. Thus, we intend to cover the examples of C# operators used among developers while using the Ubuntu 20.04 Linux system. Ensure that you have the “mcs” compiler and “mono” runtime executor for the C# language. ## Example 01: Arithmetic Operators

Let’s get started with the Arithmetic operators first. To make our code ready, we need to add the most important “System” library in the C# code. After this, you can try using any namespace. But, we have been avoiding it and directly using a user-defined class, “Test”. Within this class, we are only utilizing the main() function. Within our main() function, we will be using arithmetic operators on some variables.

So, we have initialized two integer variables, “a” and “b,” with values 14 and 7 at the starting point within our first Console.WriteLine() function statement, we have been displaying the original values of variables “a” and “b”. After this, we have been utilizing the “+” operator between “a” and “b” to calculate their sum and display it on the shell. Just like this, we have been using the “-“ operator to subtract “b” from “a”, the “*” operator to multiply the value of “a” with value of “b”, the “/” operator to divide value “14” of “a” by value “7” of “b”.

In the end, the modulus of value “a” with “b” has been calculated using the “%” operator. Our program is now ready for use. Let’s just quickly save it first and then move towards the compilation. We have compiled our C# code with the C# mcs compiler for Ubuntu 20.04. After this, an “exe” file got created in our current home directory. We have to use this “exe” file in the “mono” runtime command to execute the compiled code. The output shows the sum “21”, subtraction result “7”, multiplication result “98”, division result “2”, and the modulus 0 of both values “a” and “b”. The arithmetic addition can also be utilized for string concatenation. So, we have initialized two string variables, “a” and “y,” with a string value. We have used the “+” operator to concatenate both variables and save their result in another string variable, “z”. We have used the Console.WriteLine() function statement to display x, y, and z on the shell. After this, we have again used the “+” operator to concatenate two strings without using any variables. After running this code exe file, we have got the separate string values for variables x and y and their concatenated value z on the shell. The last concatenated string is also displayed. The “+” and “-“ operators are also utilized in programming for increment and decrement purposes, especially in loops. So, we have been applying the post-increment, post-decrement, pre-increment, and pre-decrement operators on two different variable values within the Console.WriteLine() function statement. The “post” increment or decrement will save or display the value of the variable first and then increment or decrement it. On the other hand, the “pre” increment or decrement operator will increment/decrement first and then display or save it. After running this code, we have got the below-shown values after increment and decrement. ## Example 02: Assignment Operators

The assignment operator “=” is used to assign or initialize a value for a specific variable. So, we will use the assignment operator and other arithmetic and logical operators to get different results. We have started this program with the same general syntax, i.e., System library, Test class, and the main() driver function.

Within the main() driver function, we have initialized a value “500” to integer variable “a” using the assignment operator “=”. The first Console.WriteLine() function statement is used to simply display the value of variable “a”. After this, we have been using the bitwise exclusive OR operator “^”, bitwise right shift operator “>>”, and bitwise left shift operator “<<” to calculate the result of shift by “3”. After this, we have been using the compound assignment operators in remaining Console.WriteLine() statements. We have been using the ‘=” operator with the “+”, “-“, “*”, “/”, “%”, “&” and “|” operators. The new value of “a” has been calculated by using all these operators with value “3”. After this c# code compilation, we have executed its exe file perfectly. Using compound assignment operators, it displayed the original value of “a” and then the calculated value of “A”. ## Example 03: Comparison Operators

Let’s get started with the comparison operators in C# now. We will be using the comparison operators to compare two variable values, “a” and “b,” with different conditions. So, we have been starting this C# code with the initialization of variables “a” and “b” with some value in the main() function. The first line of the Console.WriteLine() method statement is used to display the original values of “a” and “b”.

After that, we have been using the other Console.WriteLine() method statements to compare both variables and get the result returned in boolean, i.e., false or true. The comparison operator “==” is utilized to find if the value of “a” is equal to the value of “b”, and the operator “!=” is utilized to find if both are not equal. The “>” operator indicated “greater than” and “<” operator “less than” condition. The compound “>=”, and “<=” are used two conditions at a time, i.e., greater or equal, less or equal. After this code “exe” file execution, we have learned that “a” is not equal to “b”. Also, we have got to know that the value of variable “a” is greater than “b” and “b” is less than “a”. ## Example 04: Logical Operators

The logical operators are used to identify the result of two or more Boolean expressions. Thus, we will do the same. We will be utilizing the AND, OR, and NOR logical operators in this C# program. We have to use at least two Boolean expressions in our Console.WriteLine() function statement to display the results. So, we have been using the “<” and “>” comparison operators between variables “a” and “b” to get the Boolean result.

After getting the result for both expressions, we will apply the AND, OR, and NOT operators between both expressions and find out the Boolean value. The “NOT” operator will reverse the result of the whole impression. We have got “False” for applying “&&”, “True” for “||” and “False” for the “!” operator. ## Conclusion

This article is all about the illustration of using different operators in C# programs. We have tried to cover very basic examples of using the arithmetic operators (addition, subtraction, multiplication, division, modulus), the logical operators (AND, OR, NOT), the comparison operators (equal, not equal, greater than, less than), and other compound operators. Along with that, we have tried to make use of these operators for increment, decrement, and string concatenation purposes. All the examples described above can be utilized in any other language. 