C# was created to deal with the increased demands of new workloads and paradigm shifts in programming. There is a slew of operators available in the C# programming language. A large number of these are provided by built-in types and can be used to execute simple operations on values of those types. It's possible to customize the operator's behavior for operands of a user-defined type by overloading those operators.
In this 'Operator in C#' tutorial, you will learn the major technical aspects of variables and fundamentals involving data types.
What Is an Operator in C#?
Operators are symbols that can be used with operands to perform tasks. It is possible to use a variable or a constant as an operand.
Operators are used to manipulating variables and values in the C# programming language. Several operators are available in C#, and they are classified based on the actions they execute.
We can now go on to the next topic to learn about the expressions now that we have a basic knowledge of the definition and complexity of operators in C#.
What Is an Expression for Operator in C#?
In C#, an expression comprises a combination of operands and operators that operate together. An expression must have at least one operand, while it may or may not contain additional components such as operators or auxiliary variables. An expression is followed by a semicolon, which results in an expression statement.
Now let's discuss various types of operators.
What Are the Types of Operators in C#?
There are five types of operators in C#
1. Assignment Operator:
Assignment operators (=) are employed when values are assigned to variables in a computer program.
Code:
int x;
x=7;
int y;
y=x;
2. Arithmetic Operators:
Arithmetic operators are used to performing arithmetic operations such as addition, subtraction, multiplication, division, etc.
Code:
int x=14;
int y=13;
int z=x+y; //z=27
3. Relational Operators:
When comparing two operands, relational operators are employed. If the relationship is true, the outcome will be true; otherwise, it will be false.
Code:
bool res;
int x = 10, y = 20;
result = (x==y);
Console.WriteLine(res);
4. Logical Operators:
Operators such as "and" & "or" are employed to execute logical operations. Logical operators evaluate boolean expressions and return boolean values (true and false). Logical operators are employed in decision-making and looping.
Code:
bool res;
int a = 10, b = 20;
res = (a == b) || (a > 5);
Console.WriteLine(res);
5. Unary Operators:
Unlike the other operators, the unary operators only have one operand, which is different from other operators.
Code:
int n = 10, res;
bool flag = true;
res = +n;
Console.WriteLine(res);
So far, we have learned about the major operators. Now let’s discuss the precedence of the operators.
What Is an Operator Precedence?
The higher precedence operators are evaluated first in an equation with multiple operators. The order of operations is determined by the associative relationship between the operands of two operators of the same precedence.
Implementation of Operators in C#?
In C#, we will implement Operators in a code editor.
Code:
using System;
namespace C__operator
{
class Program
{
static void Main(string[] args)
{
int x, y, num1 = 25, num2 = 3, rem, res;
bool value1;
// Assigning a constant to variable
x = 10;
Console.WriteLine("x={0}", x);
// Assigning a variable to another variable
y = x;
Console.WriteLine("y={0}", y);
// Addition operator
res = x + y;
Console.WriteLine("{0}+{1} = {2}", x, y, res);
// Subtraction operator
res = x - y;
Console.WriteLine("{0}-{1} = {2}", x, y, res);
// Multiplication operator
res = x * y;
Console.WriteLine("{0}*{1} = {2}", x, y, res);
// Division operator
res = x / y;
Console.WriteLine("{0}/{1} = {2}", x, y, res);
// Modulo operator
rem = num1 % num2;
Console.WriteLine("{0}%{1} = {2}", num1, num2, rem);
value1 = (x == y);
Console.WriteLine("{0}=={1} returns {2}",x, y, value1);
value1 = (x > y);
Console.WriteLine("{0}>{1} returns {2}",x, y, value1);
value1 = (x < y);
Console.WriteLine("{0}<{1} returns {2}", x, y, value1);
x=30;
y=10;
// OR operator
value1 = (x == y) || (x > 5);
Console.WriteLine(value1);
// AND operator
value1 = (x == y) && (x > 5);
Console.WriteLine(y);
int z = 10, value2;
bool flag = true;
value2 = +z;
Console.WriteLine("+z = " + value2);
value2 = -z;
Console.WriteLine("-z = " + value2);
value2 = ++z;
Console.WriteLine("++z = " + value2);
value2 = --z;
Console.WriteLine("--z = " + value2);
Console.WriteLine("!flag = " + (!flag));
}
}
}
By now, you have a good grip on the technical aspects of Operators in C#.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
Next Steps
The next lesson in your C# training can be "Methods in C#." C# methods are blocks of code or statements that allow the user to reuse code save memory and time while also increasing the readability of the code, among other things.
Simplilearn is the world's most popular online Bootcamp for learning digital economy skills, and it's here to help you do just that. Digital marketing and data science are just a few subjects we cover in-depth in our online courses.
The Caltech CTME and the Indian Institute of Technology, Kanpur, in collaboration with Simplilearn to deliver their Software Development courses. In addition to more advanced topics like Competitive Programming, these courses teach the fundamentals of data structures and algorithms. As a software developer, data structures including trees, graphs, and queues will be taught.
The comments section below is open for questions and comments about this 'Operators in C#' tutorial. Happy learning!