WHY SHOULD YOU OVERRIDE EQUALS METHOD
1. WHAT IS EQUALS METHOD
2. WHY SHOULD YOU OVERRIDE IT.
using System;
namespace ConsoleApplication4
{
public class MainClass
{
public static void Main()
{
Customer C1 = new Customer();
C1.FirstName = "Malla";
C1.LastName = "Gurram";
Customer C2 = new Customer();
C2.FirstName = "Malla";
C2.LastName = "Gurram";
Console.WriteLine(C1 == C2);
Console.WriteLine(C1.Equals(C2));
Console.ReadLine();
}
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override bool Equals(object obj)
{
if(obj == null)
{
return false;
}
if(!(obj is Customer))
{
return false;
}
return this.FirstName == ((Customer)obj).FirstName &&
this.LastName == ((Customer)obj).LastName;
}
public override int GetHashCode()
{
return this.FirstName.GetHashCode() ^ this.LastName.GetHashCode();
}
}
}
namespace ConsoleApplication4
{
public class MainClass
{
public static void Main()
{
Customer C1 = new Customer();
C1.FirstName = "Malla";
C1.LastName = "Gurram";
Customer C2 = new Customer();
C2.FirstName = "Malla";
C2.LastName = "Gurram";
Console.WriteLine(C1 == C2);
Console.WriteLine(C1.Equals(C2));
Console.ReadLine();
}
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override bool Equals(object obj)
{
if(obj == null)
{
return false;
}
if(!(obj is Customer))
{
return false;
}
return this.FirstName == ((Customer)obj).FirstName &&
this.LastName == ((Customer)obj).LastName;
}
public override int GetHashCode()
{
return this.FirstName.GetHashCode() ^ this.LastName.GetHashCode();
}
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.