C# Operators

C# Code Snippets Operators
Share:

About

In this code snippet, we’ll take a look at the operators present in C#.

An operator is a symbol or a set of symbols that represent a specific function that will be performed on the operands(our values or variables). There are multiple types of operators: arithmetic, logical, comparison … In this post, we will take a look at some of the most basic ones. Here is a complete list of all the operators and their functionalities.

Let’s look at the code example below to see how to use operators.

Code:

using System;

namespace operators
{
    class Program
    {
        static void Main(string[] args)
        {
            int myVariable;

            //Assignment operator.
            int a = 5;
            int b = 2;

            //Arithemntic operators//////////////////////////////////////

            //Addition operator.
            myVariable = a + b;
            Console.WriteLine(myVariable);

            //Subtraction operator.
            myVariable = a - b;
            Console.WriteLine(myVariable);

            //Multiplication operator.
            myVariable = a * b;
            Console.WriteLine(myVariable);

            //Division operator.
            myVariable = a / b;
            Console.WriteLine(myVariable);

            //Increment by one operator.
            myVariable = a++ ;
            Console.WriteLine(myVariable);

            //Decrement by one operator.
            myVariable = b--;
            Console.WriteLine(myVariable);

            //Addition
            myVariable = 10;

            myVariable = myVariable + a;
            //The code above is the same the code below:
            myVariable += a;
        
            Console.WriteLine(myVariable);

            //////////////////////////////////////////////////////////



            //Comparison Operators////////////////////////////////////

            // ==               : equals to
            //!=                : doesn't equal
            //< >               : bigger/smaller than
            //<= >=             : bigger or equal/smaller or equal than
            //!myBoolVariable   : not

            //An example of how to use a comparison operator.  
            a = 10;
            b = 5;

            if (a > b)
            {
                Console.WriteLine("a is bigger than b");
            }
            else
            {
                Console.WriteLine("b is bigger than a.");
            }


            //////////////////////////////////////////////////////////



            //Logical Operators///////////////////////////////////////

            //&&                : and
            //||                : or

            //An example of how to use a logical operator.
            a = 10;
            b = 5;
            bool myBool = true;

            if ((a > b) && myBool) //Just like in math the expression in the () will be evaluated first.
            {
                Console.WriteLine("a is bigger than b AND myBool is true.");
            }


            //////////////////////////////////////////////////////////

            Console.ReadLine();
        }
    }
}

Resulting output:

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger