pcbway




Common Operators in C#, arithmetic operator in c#, comparison operator in c#, conditional operators in c#Hello friends, I hope you all are doing great. In today’s tutorial, we are going to have a look at few Common Operators in C#. It’s 4th tutorial in C# series and before going forward, we have to first understand the working operation of these C# operators.

We have a lot of operators in C# but I have discussed few of them, which are most commonly used. If you got into any trouble then ask in comments. So, let’s get started with Common Operators in C#:

Common Operators in C#

  • Operators are used to create a link, relation or operation between two entities/variables. Few of these C# operators are as follows:
    • Assignment Operator ( = )
    • Arithmetic Operators ( + , – , * , / , % )
    • Comparison Operators ( == , != , > , < , >= , <= )
    • Conditional Operators ( && , | | )
    • Ternary Operator ( ? : )
    • Null Coalescing Operator ( ?? )
    • Escape Sequence ( / )
    • Verbatim Literal ( @ )
  • Let’s discuss them one by one in detail:
allpcb
1. Assignment Operator ( = )
  • Assignment Operator is used to assign a value from one entity to another.
  • Let’s say we initialize an integer with value 5, so what we need to write is int i = 5; so this assignment operator has assigned a value of 5 to integer i.
2. Arithmetic Operators ( + , – , * , / , % )
  • Arithmetic Operators ( + , – , * , / , % ) are used for performing different mathematical operations between two entities/variables.
  • Each arithmetic operator has its own mathematical operation associated with it. For example:
    • + is used to add two numbers i.e. int a = 5 + 10; so compiler will first apply the arithmetic operator (+) and will add 5 & 10 and after that will use assignment operator to assign the value 15 to variable a.
    • – is used to subtract two numbers i.e. int b = 10 – 5; result will be 5.
    • * is use to multiply two numbers i.e. int c = 10 * 5; result will be 50.
    • / is used to divide two numbers i.e. int d = 10 / 2; result will be 5.
    • % is used to get the remainder of two numbers i.e. int e = 22 / 4; result will be 2.
  • Here’s one of the arithmetic operator in action:

Common Operators in C#, arithmetic operator in c#




3. Comparison Operators ( == , != , > , < , >= , <= )
  • Comparison Operators ( == , != , > , < , >= , <= ) are used to compare two entities with one another.
  • We will discuss them in detail in Loops section while discussing if loop.
    • a == b, it will check whether a is equal to b.
    • a != b, a is not equal to b.
    • a > b, a is greater than b.
    • a < b, a is less than b.
    • a >= b, a is greater than or equal to b.
    • a <= b, a is less than or equal to b.
  • Here’s one of the comparison operator used in IF Loop, I am comparing the total with 10, if Total is greater than 10 it will give us the value.

Common Operators in C#, arithmetic operator in c#, comparison operator in c#

4. Conditional Operators ( && , | | )
  • Conditional Operators ( && , | | ) are used to create a relation between two conditions.
  • This one will also be discussed in more detail in IF Loops section.
    • && , It is pronounced as AND, this operator makes sure that both conditions must be true.
    • | | , It is pronounced as OR, this operator returns TRUE if either of the two conditions is true.
  • I have placed a conditional operator in below figure, I have placed a check that value must be greater than 10 and less than 25.

Common Operators in C#, arithmetic operator in c#, comparison operator in c#, conditional operators in c#



5. Ternary Operator ( ? : )
  • Ternary operator is one of the coolest feature of C# and comes quite handy at times.
  • It’s a simple form of if loop, which reduces the IF Else Loop code in single line. We will discuss it in If loop section.
6. Null Coalescing Operator ( ?? )
  • Null Coalescing Operator ( ?? ) is used to convert nullable value into non-nullable value.
  • Let’s say we have two integers a and b defined as:

int? a = 15; (nullable variable)

int b = 0; (non-nullable variable)

  • Now we want to save value of a into b so if you write a = b; compiler will generate an error.
  • So, inorder to do that you need to use Null Coalescing Operator ( ?? ) as follow:

int b = a ?? 0;

  • if the value of a is null, then b will become 0. It’s called the default value, you can set it to anything, I’ve made it 0.
Complete Code
  • Here’s the complete code used in this tutorial:

using System;

namespace TEPProject
{
class Program
{
static void Main(string[] args)
{
Console.Write(“Enter Numer 1: “);
int Num1 = int.Parse(Console.ReadLine());
Console.Write(“Enter Numer 2: “);
int Num2 = int.Parse(Console.ReadLine());

int Total = Num1 + Num2;

if (Total > 10 && Total < 25)
{
Console.WriteLine(“Total count is {0}”, Total);
}
else
{
Console.WriteLine(“Total count is less than 10 or greater than 25”);
}
}
}
}

I hope you have learnt something from today’s tutorial. Let me know if you have any questions in comments. Take care & have fun !!! 🙂

Leave a Reply