C # operator


Release date:2023-08-23 Update date:2023-10-13 Editor:admin View counts:261

Label:

C # operator

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. C # has a wealth of built-in operators, classified as follows:

  • Arithmetic operator

  • Relational operator

  • Logical operator

  • Bit operator

  • Assignment operator

  • Other operators

This tutorial will cover arithmetic operators, relational operators, logicaloperators, bit operators, assignment operators, and other operators one by one.

Arithmetic operator

The following table shows all the arithmetic operators supported by C #. Hypothetical variable A, value is 10, variable B is 20, then:

Operator

Description

Example

+

Add up the two operands

A + B will get 30

-

Subtract the second Operand from the first Operand

A-B will get-10

*

Multiply two operands

A * B will get 200

/

Numerator divided by denominator

B / A will get 2.

%

Take the modular operator, the remainder after division

B% A will get 0

++

Self-increment operator, the integer value increases by 1

Atom + will get 11.

--

Self-subtraction operator, integer value minus 1

Amure-will get 9

Example

Take a look at the following example for all the arithmetic operators available in C#:

Example

using System;
namespace OperatorsAppl
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 21;
            int b = 10;
            int c;
            c = a + b;
            Console.WriteLine("The value of Line 1- c is {0}", c);
            c = a - b;
            Console.WriteLine("The value of Line 2- c is {0}", c);
            c = a * b;
            Console.WriteLine("The value of Line 3- c is {0}", c);
            c = a / b;
            Console.WriteLine("The value of Line 4- c is {0}", c);
            c = a % b;
            Console.WriteLine("The value of Line 5- c is {0}", c);
            // ++a Perform autoincrement operation before assigning values
            c = ++a;
            Console.WriteLine("The value of Line 6- c is {0}", c);
            // At this point, the value of a is 22
            // --a Perform self subtraction before assigning values
            c = --a;
            Console.WriteLine("The value of Line 7- c is {0}", c);
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

The value of Line 1- c is 31
The value of Line 2- c is 11
The value of Line 3- c is 210
The value of Line 4- c is 2
The value of Line 5- c is 1
The value of Line 6- c is 22
The value of Line 7-c is 21
  • c = a++ : First assign a to c, and then perform a self-increment operation on a

  • c = ++a : First perform the self-increment operation of a, and then assign a to c

  • c = a-- : First assign a to c, and then self-subtract a.

  • c = --a : Self-subtract a first, and then assign a to c

Example

using System;
namespace OperatorsAppl
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 1;
            int b;
            // a++ Assign values first and then perform autoincrement operation
            b = a++;
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);
            Console.ReadLine();
            // ++a Perform autoincrement operation before assigning values
            a = 1; // Reinitialize a
            b = ++a;
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);
            Console.ReadLine();
            // a-- Assign values first and then perform self subtraction operations
            a = 1;  // Reinitialize a
            b= a--;
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);
            Console.ReadLine();
            // --a Perform self subtraction before assigning values
            a = 1;  // Reinitialize a
            b= --a;
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);
            Console.ReadLine();
        }
    }
}

Powered by TorCMS (https://github.com/bukun/TorCMS).