Jan 15, 2020

OPERATOR OVERLOADING IN C#

operator overloading, c# operator overloading, overloading,operator overloading c#
operator overloading 

OPERATOR OVERLOADING

            “The approaches to defining multiple behaviors to an operator, the behavior will vary based on the operand used between the operators’”

For examples

            Let take the ‘+’ addition operator when it is used between two numeric operand it play the role of addition and when it use between two string operand it play concatenation. Like this
            Number + Number => addition
            String + String        => concatenation    // concatenation operator join the string.
Note: we cannot apply ‘string – string ‘ it is invalid and compiler give you error. Because for the ‘-‘ of two string inside compiler no class and logic are defined.

Code Example


class OperatorTest
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
        public string Join(string a, string b)
        {
            return a + b;
        }
operator overloading in c++, operator overloading example, define operator overloading,method overloading and operator overloading
difference between method
and operator overloading
        static void Main()
        {
            int a, b;

            Console.WriteLine("enter sentence one");
            string str1 =Console.ReadLine();
            Console.WriteLine("enter sentence two");
            string str2 = Console.ReadLine();
            if (str1 != null)
                a = 1;
            else
                a = 0;
            if (str2 != null)
               b = 1;
            else
                b = 0;
            OperatorTest o = new OperatorTest();
            Console. WriteLine( o.Join(str1, str2));
            Console.WriteLine("totel {0} statement are joined", o.Add(a, b));
            Console.ReadKey();
        }
    }

Method overloading, Constructor overloading & Operator overloading

Method overloading
    Method overloading when we make more than one method with same name and signature. But different behavior.
  Here Overloading is done by having different parameter for each and every method.
Constructor overloading
      Constructor overloading when we make more than one constructor with same name and signature. But different behavior.
   Here Overloading is done by having different parameter for each and every constructor.
Operator overloading
   Operator overloading is raise when an operator is used more than one times with different behavior.
   Operator overloading is done by changing operand used in between the operator.


0 comments:

Post a Comment

Please do not enter any spam link in the comment box