overriding equals
-
public class User{
public int UserID {get;set;}
public string Username {get;set;}publict override bool Equals(object obj)
{
if(!(obj is User)) return false;
return (obj as User).UserID == UserID;
}public override int GetHashCode()
{
//UserID is form the database
return UserID;
}public override ToString()
{
return Username;
}
}base on the code above, when i create to object from the database, comparing them will return false even they are the same. example:
User u1 = new User(){UserID = 1, Username = "FirstUser"};
User u2 = new User(){UserID = 1, Username = "FirstUser"};var b = u1 == u2;
variable b is false; I thought they will be the same, is there i missing there? I will appreciate for any advice will be given Thank you.
-
public class User{
public int UserID {get;set;}
public string Username {get;set;}publict override bool Equals(object obj)
{
if(!(obj is User)) return false;
return (obj as User).UserID == UserID;
}public override int GetHashCode()
{
//UserID is form the database
return UserID;
}public override ToString()
{
return Username;
}
}base on the code above, when i create to object from the database, comparing them will return false even they are the same. example:
User u1 = new User(){UserID = 1, Username = "FirstUser"};
User u2 = new User(){UserID = 1, Username = "FirstUser"};var b = u1 == u2;
variable b is false; I thought they will be the same, is there i missing there? I will appreciate for any advice will be given Thank you.
You need to override the == and != operators.
public static bool operator ==(User lhs, User rhs)
{
if(lhs == null && rhs == null) return true;
if(lhs == null && rhs != null) return false;
if(lhs != null && rhs == null) return false;
return lhs.UserID == rhs.UserID;
}public static bool operator !=(User lhs, User rhs)
{
return !(lhs == rhs);
}Just insert these into the class and you will be able to use == and != to check for equality and inequality. Note that if you override one, C# forces you to override the other. There are several other operators you can override. You can get all the nitty gritty details on MSDN[^].
What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???
-
public class User{
public int UserID {get;set;}
public string Username {get;set;}publict override bool Equals(object obj)
{
if(!(obj is User)) return false;
return (obj as User).UserID == UserID;
}public override int GetHashCode()
{
//UserID is form the database
return UserID;
}public override ToString()
{
return Username;
}
}base on the code above, when i create to object from the database, comparing them will return false even they are the same. example:
User u1 = new User(){UserID = 1, Username = "FirstUser"};
User u2 = new User(){UserID = 1, Username = "FirstUser"};var b = u1 == u2;
variable b is false; I thought they will be the same, is there i missing there? I will appreciate for any advice will be given Thank you.
You might also want to check for
null
being passed in to theEquals
method. i.e. insert the following line into the beginning of the method:if(other == null) return false;
What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???
-
You need to override the == and != operators.
public static bool operator ==(User lhs, User rhs)
{
if(lhs == null && rhs == null) return true;
if(lhs == null && rhs != null) return false;
if(lhs != null && rhs == null) return false;
return lhs.UserID == rhs.UserID;
}public static bool operator !=(User lhs, User rhs)
{
return !(lhs == rhs);
}Just insert these into the class and you will be able to use == and != to check for equality and inequality. Note that if you override one, C# forces you to override the other. There are several other operators you can override. You can get all the nitty gritty details on MSDN[^].
What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???
thank you for the quick reply. I thought overriding equals will be enough, By the way, one more question base on the code above, which is most efficient using the "Equals" method or the equals operator? Thank you
-
thank you for the quick reply. I thought overriding equals will be enough, By the way, one more question base on the code above, which is most efficient using the "Equals" method or the equals operator? Thank you
-
You need to override the == and != operators.
public static bool operator ==(User lhs, User rhs)
{
if(lhs == null && rhs == null) return true;
if(lhs == null && rhs != null) return false;
if(lhs != null && rhs == null) return false;
return lhs.UserID == rhs.UserID;
}public static bool operator !=(User lhs, User rhs)
{
return !(lhs == rhs);
}Just insert these into the class and you will be able to use == and != to check for equality and inequality. Note that if you override one, C# forces you to override the other. There are several other operators you can override. You can get all the nitty gritty details on MSDN[^].
What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???
As you're dealing with a
class
, not astruct
, you can simplify the operator to:public static bool operator ==(User lhs, User rhs)
{
return Equals(lhs, rhs);
}public static bool operator !=(User lhs, User rhs)
{
return !Equals(lhs, rhs);
}The static
Equals
method[^] will perform the null checks for you, and then call the instanceEquals
method on the first parameter:public static bool Equals(Object objA, Object objB)
{
if (objA==objB) {
return true;
}
if (objA==null || objB==null) {
return false;
}
return objA.Equals(objB);
}At first glance, it might look like the first line of that method would cause a stack overflow, since your
==
operator is calling a method that calls the==
operator. However, within theEquals
method, it's calling the==
operator on theObject
type, which just tests for reference equality.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
As you're dealing with a
class
, not astruct
, you can simplify the operator to:public static bool operator ==(User lhs, User rhs)
{
return Equals(lhs, rhs);
}public static bool operator !=(User lhs, User rhs)
{
return !Equals(lhs, rhs);
}The static
Equals
method[^] will perform the null checks for you, and then call the instanceEquals
method on the first parameter:public static bool Equals(Object objA, Object objB)
{
if (objA==objB) {
return true;
}
if (objA==null || objB==null) {
return false;
}
return objA.Equals(objB);
}At first glance, it might look like the first line of that method would cause a stack overflow, since your
==
operator is calling a method that calls the==
operator. However, within theEquals
method, it's calling the==
operator on theObject
type, which just tests for reference equality.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thank you, for all the hint, i finally got it now