Tuesday 6 March 2012

Overloading Equality Operator


//Program to Oveload == Operator
using System;

class Student
{
int age;
string name;

//Constrors to initialize age and name
public Student(int age,string name)
{
this.age=age;
this.name=name;
}

// Operator overloading
public static bool operator ==(Student a1,Student a2)
{
if((a1.age==a2.age) && (a2.name==a1.name))
return true;
else
return false;
}

public static bool operator !=(Student a1,Student a2)
{
if((a1.age!=a2.age) && (a2.name!=a1.name))
return false;
else
return true;
}
}

class Program
{
static void Main()
{
Student L1=new Student(26,"Ankur");

Student L2=new Student(26,"Bhatnagar");

if(L1==L2)
Console.WriteLine("L1 and L2 are equal");
else
Console.WriteLine("L1 and L2 are not equal");
}
}

No comments:

Post a Comment