Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Operator Overloading

Operator overloading is a compile-time polymorphism


in which the operator is overloaded to provide the
special meaning to the user-defined data type. 

Operator overloading gives the ability to use the same


operator to do various operations.

Every operator has a predetermined behavior. You can


change the default behavior of an operator using
operator overloading.
It provides additional capabilities to C# operators
when they are applied to user-defined data types.
Only the predefined set of C# operators can be
overloaded.
Overloaded operators are functions with special
names the keyword operator followed by the
symbol for the operator being defined.
Similar to any other function, an overloaded
operator has a return type and a parameter list.
The advantage of Operators overloading is to
perform different operations on the same operand.
Syntax :

access specifier className operator


Operator_symbol (parameters)
{
// Code
}
Operator method must be public and static
Operator parameter must not use the ref and out
modifier.
The following table describes the overloading
ability of the various operators available in C# :
Overloading Binary Operators

Binary Operators will work with two Operands.


Examples of binary operators include the Arithmetic
Operators (+, -, *, /, %) and  Relational Operators etc.
Overloading a binary operator is similar to overloading a
unary operator, except that a binary operator requires an
additional parameter.
The following syntax shows the use of Binary operator –
operator operator(object1, object2);
Here, second "operator" is a symbol that denotes a binary
operator.
 operator + (obj1, obj2);
Example :

using System;
class demo
{
public int num1=0;
public string str1="";
public static demo operator+(demo d1,demo d2)
{
demo d3=new demo();
d3.num1=d1.num1+d2.num1;
d3.str1=d1.str1+d2.str1;
return d3;
}
}
class demo1
{
public static void Main()
{
demo d1=new demo();
d1.num1=10;
d1.str1="hello";

demo d2=new demo();


d2.num1=20;
d2.str1="Students";

demo d3=new demo();


d3=d1+d2;
Console.Write("sum of two integer:-"+d3.num1);
Console.WriteLine();
Console.Write("Concate twostring:-"+d3.str1);

}
}
Overloading Unary Operator

A unary +,-,!,or ~ operator must take a single parameter of its class


type and can return any type.
A unary ++ or -- operator must take a single parameter of its class type
and can return any type.
A unary true or false operator must take a single parameter of its class
type and return type must be bool.
But do remember that the true and false operators can be overloaded
as pairs only. The compilation error arises if a class declares one of
these operators without declaring the other.
The following syntax shows the use of Unary operator –
Operator operator (object);
here, operator is a symbol that denotes a unary operator.
operator +(obj1);
Example 1:

using System;
class demo
{
public int num1,num2;
public static demo operator-(demo d1)
{
d1.num1= -d1.num1;
d1.num2= -d1.num2;
return d1;
}
}
class demo1{
public static void Main(){
demo d1=new demo();
d1.num1=-10;
d1.num2=20;
Console.WriteLine("Before call operator");
Console.WriteLine("value1 :"+d1.num1);
Console.WriteLine("value2 :"+d1.num2);
d1=-d1;
Console.WriteLine("After call operator");
Console.WriteLine("value1 :"+d1.num1);
Console.WriteLine("value2 :"+d1.num2);
}
}
Example 2:
using System; public static void Main()
class demo {
{ demo d1=new demo();
bool x=false; demo d2=new demo();
public void set(bool v) d1.set(true);
{ d2.set(false);
x=v; Console.WriteLine("value1 :"+!d1);
} Console.WriteLine("value1 :"+!d2);
public static bool operator !(demo d)
{ }
return !(d.x);
} }
Conversion Operator

Conversion operators have the following properties :


1. Conversions declared as implicit occur
automatically when it is required.
 For example, converting an int to a long can occur as a normal
assignment operation.
2. Conversions declared as explicit require a cast to be
called.
 You can convert a class type to any CLS type and
one class type to another class type using
overloading of conversion operator.
1.Conversion of a class type to CLS type(Implicit)

using System;
class demo
{
int cnt;
public demo()
{
cnt=0;
}
public static demo operator ++(demo d1)
{
d1.cnt++;
return d1;
}
public static implicit operator int(demo d1)
{
return d1.cnt;
}
public static void Main()
{
demo d1=new demo();
long i;
d1++;
i=d1;
Console.WriteLine("value of i:"+i);
}
}
Conversion of a class type to CLS type(Explicit)

using System;
class demo
{
long cnt;
public demo()
{
cnt=0;
}
public static demo operator ++(demo d1)
{
d1.cnt++;
return d1;
}
public static explicit operator long(demo d1)
{
return d1.cnt;
}
public static void Main()
{
demo d1=new demo();
int i;
d1++;
i=(int)d1;
Console.WriteLine("value of i:"+i);
}
}
2.Conversion of one class type to other class
type(Explicit)

using System;

class Apartment
{
public string name;
public static explicit operator House(Apartment a)
{
House h1=new House();
h1.name=a.name;
return h1;
}
class House
{
public string name;
public static explicit operator Apartment(House h)
{
Apartment a1=new Apartment();
a1.name=h.name;
return a1;
}
}
class Program
{
static void Main()
{
House h = new House();
h.name = "Dharmnandan";
// Cast a House to an Apartment.
Apartment a = (Apartment)h;
// Apartment was converted from House.
Console.WriteLine(a.name);
}
}

You might also like