For example, a variable can be of the type String, which means that it will be used to store a string value. Based on the data type, specific operations can be carried out on the variable. For instance, if we had an Integer variable, then operations such as addition and subtraction can be carried out on the variable. One can declare multiple variables in a program. Let’s look at a quick example of the declaration of multiple variables of different data types. In our example, we will define two variables, one of the type ‘string’ and the other of the type ‘Integer’. We will then display the values of these variables to the console. For each example, we will modify just the main function in our Program.cs file.
Code Explanation
A variable of the data type String is declared. The name of the variable is ‘message’. The value of the variable is “The value is “. A variable of the data type Integer (Int32) is declared. The name of the variable is ‘val’. The value of the variable is 30. Finally the Console.write statement is used to output both the value of the String and Integer variable.
If the above code is entered properly and the program is executed successfully, the following output will be displayed.
Output
From the output, you can see that the values of both the string and integer variable are displayed to the console. Operators are used to performing operations on values of various data types. For example, to perform the addition of 2 numbers, the + operator is used. Let’s see the table of operators available for the various data types
C# Operators
Arithmetic Operators
These are operators used for performing mathematic operations on numbers. Below is the list of operators available in C#.
Relational Operators
These are operators used for performing Relational operations on numbers. Below is the list of relational operators available in C#.
Logical Operators
These are operators used for performing Logical operations on values. Below is the list of operators available in C#. Let’s look at a quick example of how the operators can be used in .Net. In our example, we will define 2 Integer variables and one Boolean variable. We will then perform the following operations
Code Explanation
Two Integer variables are defined, one being val1 and the other being val2. These will be used to showcase relational and arithmetic operations. A Boolean variable is defined to showcase logical operations. An example of the arithmetic operation is shown wherein the addition operator is carried out on val1 and val2. The result is written to the console. An example of the relational operation is shown wherein the less than operator is carried out on val1 and val2. The result is written to the console. An example of the logical operation is shown, wherein the logical operator (!) is applied to the status variable. The logical NOT operator reverses the current value of any Boolean value. So if a Boolean value is ‘true’, the logical NOT will return the value ‘false’ and vice versa. In our case since the value of the status variable is ‘true’, the result will show ‘false’. The result is written to the console.
If the above code is entered properly and the program is executed successfully, the output will be displayed.