GetHashCode for the object with several data members - c#

public class MyClass
{
public string x;
public string y;
}
public class MyClassEqualityComparer : IEqualityComparer<MyClass>
{
public int GetHashCode(MyClass myobj)
{
if(myObj == null)
{
return base.GetHashCode();
}
if (myObj.x != null && myObj.y != null)
{
return myObj.x.GetGashCode()^myObj.y.GetGashCode();
}
}
}
what should be the implementation if myObj.x or/and myObj.y are nulls

The only requirement for a hash code is that two objects that are considered equal share the same hash code.
You can, for example, use 0 for null properties
public int GetHashCode(MyClass myobj)
{
if(myObj == null)
{
return base.GetHashCode();
}
return (myObj.x != null ? myObj.x.GetGashCode() : 0) ^ (myObj.y != null ? myObj.y.GetGashCode() : 0)
}

Related

How do you write a GetHashCode method for an object made of a string and a collection of int32?

There is a class of Products:
public class ProductWithFeatures
{
public string Name { get; set; }
public ICollection<Feature> Features { get; set; }
}
public class Feature
{
public int Id { get; set; }
public Feature(int Id)
{
this.Id = Id;
}
}
I want to write an IEqualityComparer for this (i already have one for Feature).
The one for Feature is like this:
public class FeatureComparer : IEqualityComparer<Feature>
{
public bool Equals(Feature x, Feature y)
{
return x.Id == y.Id;
}
public int GetHashCode(Feature obj)
{
return obj.Id;
}
}
And what i wrote so far on the other one is this:
public class ProductComparer : IEqualityComparer<LinqHomework.ProductWithFeatures>
{
public bool Equals(ProductWithFeatures x, ProductWithFeatures y)
{
return x.Name == y.Name && LinqHomework.FeatureComparer.Equals(x.Features, y.Features);
}
public int GetHashCode(ProductWithFeatures obj)
{
}
}
I can't find an answer anywhere about this. Does anybody know how to write it?
Two ProductWithFeaturess are equal if they have the same name, and have the same features in the same order.
public class ProductComparer : IEqualityComparer<LinqHomework.ProductWithFeatures>
{
public bool Equals(ProductWithFeatures x, ProductWithFeatures y)
{
return x.Name == y.Name && x.Features.SequenceEqual(y.Features, new LinqHomework.FeatureComparer());
}
public int GetHashCode(ProductWithFeatures obj)
{
int hash = obj.Name.GetHashCode();
var featureComparer = new LinqHomework.FeatureComparer();
foreach (var feature in obj.Features)
{
hash = hash * 23 + featureComparer.GetHashCode(feature);
}
return hash;
}
}
This is a simple approach, which can be improved in a number of ways.
First, let's give our FeatureComparer a Default property, so we don't need to keep creating new instances:
public class FeatureComparer : IEqualityComparer<Feature>
{
public static FeatureComparer Default { get; } = new FeatureComparer();
// ... as before
}
This lets us write:
public class ProductComparer : IEqualityComparer<LinqHomework.ProductWithFeatures>
{
public bool Equals(ProductWithFeatures x, ProductWithFeatures y)
{
return x.Name == y.Name && x.Features.SequenceEqual(y.Features, LinqHomework.FeatureComparer.Default);
}
public int GetHashCode(ProductWithFeatures obj)
{
int hash = obj.Name.GetHashCode();
foreach (var feature in obj.Features)
{
hash = hash * 23 + LinqHomework.FeatureComparer.Default.GetHashCode(feature);
}
return hash;
}
}
We're also not handling the case where our methods are passed null, or the name of a feature is null, so let's deal with those. We can also test whether x and y are the same object in Equals.
We'll also do the integer operations in an unchecked block in case it overflows (and the assembly is compiled with /checked).
Note that we use ReferenceEquals instead of ==, in case you end up implementing the == operator in your types.
public class ProductComparer : IEqualityComparer<LinqHomework.ProductWithFeatures>
{
public bool Equals(ProductWithFeatures x, ProductWithFeatures y)
{
if (ReferenceEquals(x, y))
return true;
if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
return false;
if (x.Name != y.Name)
return false;
if (ReferenceEquals(x.Features, y.Features))
return true;
if (ReferenceEquals(x.Features, null) || ReferenceEquals(y.Features, null))
return false;
if (!x.Features.SequenceEquals(y.Features, LinqHomework.FeatureComparer.Default))
return false;
return true;
}
public int GetHashCode(ProductWithFeatures obj)
{
if (ReferenceEquals(obj, null))
return 0;
unchecked
{
int hash = obj.Name?.GetHashCode() ?? 0;
if (!ReferenceEquals(obj.Features, null))
{
foreach (var feature in obj.Features)
{
hash = hash * 23 + LinqHomework.FeatureComparer.Default.GetHashCode(feature);
}
return hash;
}
}
}
}
It's really up to you. I personally would go for something like
public int GetHashCode( ProductWithFeatures obj )
{
string toHash = obj.Name;
foreach( var feature in obj.Features )
toHash += feature.GetHashCode();
return toHash.GetHashCode();
}
It's not the nicest code ever, but it does what it's supposed to do.

C# How can I compare two lists of objects in the Equals()-Method

I have two Lists of objects with the same elements and values:
parameters = new List<Parameter>() { new Parameter() { parameterName="value", parameterType="string"} }
the Parameter class looks like this:
public class Parameter
{
public string parameterName;
public string parameterType;
}
and I want to compare it with an identical List of objects with the same Elements and Values, via an unit-test.
my Method Class(I created before Method objects, which have stored a List of Parameter objects):
public class Method
{
public List<Parameter> parameters { get; set;}
public string modifier { get; set; }
public string name { get; set; }
public string type { get; set; }
public override bool Equals(object obj)
{
return this.modifier == ((Method)obj).modifier && this.name == ((Method)obj).name && this.type == ((Method)obj).type
&& this.parameters == ((Method)obj).parameters;
}
}
my problem is in the Equals method, at the point of this.parameters == ... :
public override bool Equals(object obj)
{
return this.modifier == ((Method)obj).modifier && this.name == ((Method)obj).name && this.type == ((Method)obj).type
&& this.parameters == ((Method)obj).parameters;
}
By the way, all other criteria in this method are working. Modifier, name and type are returning the right value compared to the object. Because of these are basic string values and the comparison is much easier. It gets more complicated when I want to do the same for the List of objects.
How can I compare two Lists of Parameter-objects in this Equals()-Method, do I need to compare the elements each(like comparing parameterName and parameterType of all objects in the list)?
You need to override Equals in your Parameter class as well as overriding GetHashCode in order to maintain hashes (otherwise you will get different hashes for 2 objects that have same values).
public class Parameter
{
public string parameterName;
public string parameterType;
public override bool Equals(object obj)
{
return parameterName == ((Parameter) obj)?.parameterName &&
parameterType == ((Parameter) obj)?.parameterType;
}
public override int GetHashCode()
{
unchecked
{
var hashCode = parameterName != null ? parameterName.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (parameterType != null ? parameterType.GetHashCode() : 0);
return hashCode;
}
}
}
And then use SequenceEqual when comparing Lists:
public class YourClass
{
public List<Parameter> parameters { get; set; }
public string modifier { get; set; }
public string name { get; set; }
public string type { get; set; }
public override bool Equals(object obj)
{
return modifier == ((YourClass)obj)?.modifier &&
name == ((YourClass)obj)?.name &&
type == ((YourClass)obj)?.type &&
( parameters == null && ((YourClass)obj)?.parameters == null ||
parameters != null && ((YourClass)obj)?.parameters != null && parameters.SequenceEqual(((YourClass)obj).parameters));
}
public override int GetHashCode()
{
unchecked
{
var hashCode = parameters != null ? parameters.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (modifier != null ? modifier.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (name != null ? name.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (type != null ? type.GetHashCode() : 0);
return hashCode;
}
}
}
Terminology: "Compare" and "Equal" are two different things, Compare means finding out an order (which one comes first), while Equals just tests yes/no equal or not (identity).
You have to define for yourself what equal means to you. Is {1;2;3} the same as {3;2,1;} ? Not only you have to define, what equal lists are, but also what equal parameters are.
For the Compare of list you could use
parameters.SequenceEquals(((other)Method)).parameters);
if order is important to you.
To compare the Parameters itself, you have to override the Equals of the parameter, or provide a custom EqualityComparer that you can pass to SequenceEquals.
Which actually does nothing else than comparing one by one, and breaking on the first mismatch.

ConcurrentBag FirstOrDefault NullCheck throws

I've got a Problem at my null check .
if (element == null)
throws
Object reference not set to an instance of an object.
Why can a simple null check fail at this Point? When i make a breakpoint at this Position, element has the value "null", but throws the exception anyway.
PS: At this Point, are no additional Threads active.
internal static ConcurrentBag<Node_Library> AddFileDetail(
this ConcurrentBag<Node_Library> list,
FileDetails file , Node<Node_Application> app)
{
var element = list.FirstOrDefault(x => file.Equals(x));
if (element == null)
{
list.Add(new Node_Library(file, app));
}
else
{
if (!element.ApplicationNodes.Contains(app))
{
element.AddNode(app);
}
}
return list;
}
EDIT: file is not null, the list is empty but not null
EDIT2: Operator and FileDetail details
public class FileDetails
{
internal string FileName { get; private set; }
internal string Name { get; private set;}
internal string Endung { get; private set; }
internal string Version { get; private set; }
internal string Produkt { get; private set; }
internal string ProduktVersion { get; private set; }
internal FileTyp Filetyp { get; private set; }
internal string Pfad { get; private set; }
public static bool operator==(FileDetails file1, Node_Library library)
{
return
file1.Version == library.Version &&
file1.Produkt == library.Produkt &&
file1.ProduktVersion == library.ProduktVersion &&
file1.FileName == library.FileName;
}
public static bool operator !=(FileDetails file1, Node_Library library)
{
return
!(file1.Version == library.Version &&
file1.Produkt == library.Produkt &&
file1.ProduktVersion == library.ProduktVersion &&
file1.FileName == library.FileName);
}
public static bool operator ==(FileDetails file1, FileDetails file2)
{
if (
file1.FileName == file2.FileName &&
file1.Produkt == file2.Produkt &&
file1.ProduktVersion == file2.ProduktVersion &&
file1.Version == file2.Version)
return true;
return false;
}
public static bool operator !=(FileDetails file1, FileDetails file2)
{
if (
file1.Name == file2.Name &&
file1.Produkt == file2.Produkt &&
file1.ProduktVersion == file2.ProduktVersion &&
file1.Version == file2.Version)
return false;
return true;
}
internal bool Equals(Node_Library file2)
{
if (file2 == null)
{
return false;
}
return (
Name == file2.Name &&
Produkt == file2.Produkt &&
ProduktVersion == file2.ProduktVersion &&
Version == file2.Version);
}
//More Stuff
}
EDIT3:
I used a breakpoint in my equal overload, but it never triggered... So the problem is maybe the FirstOrDefault?
finally:
Operatoroverload was faulty. Fixed it. Ty a lot.
The problem lies in your operators:
public static bool operator==(FileDetails file1, Node_Library library)
{
return
file1.Version == library.Version &&
file1.Produkt == library.Produkt &&
file1.ProduktVersion == library.ProduktVersion &&
file1.FileName == library.FileName;
}
When you compare an instance to null it's trying to access the properties Version, Produkt, ProduktVersion and FileName from a null instance, hence the NullReferenceException.
So, answering to your initial question "Why can a simple null check fail at this Point?", it's because there's nothing simple about that null check once you override the operators. =D
To solve it, you can add a null check on file2:
public static bool operator ==(FileDetails file1, FileDetails file2)
{
if (file2 != null &&
file1.FileName == file2.FileName &&
file1.Produkt == file2.Produkt &&
file1.ProduktVersion == file2.ProduktVersion &&
file1.Version == file2.Version)
return true;
return false;
}
The problem may be the previous line:
var element = list.FirstOrDefault(x => file.Equals(x));
The file parameter is probably null.
EDIT: If file is not null, then the FileDetails.Equals method can be the culprit.

Comparing (specific) object properties in c#

Building upon this answer for comparing objects in C#
Comparing object properties in c#
Knowing this is a complex topic, I wanted to handle a few more specific structures.
While this code will match properties if they are simple value types such as this object:
public class BasicStuff
{
public int anInt { get; set; }
public bool aBool { get; set; }
}
But as soon as it gets any more complex, this code fails.
So what I would like to do is make it a bit more usable for nested objects of the above, such as:
public class NestedStuff
{
public BasicStuff theBasic { get; set; }
}
Any array of the above, such as:
public class ArrayStuff
{
public BasicStuff[] theBasicArray { get; set; }
}
And finally any combination of the above:
public class AllTheStuff
{
public int anInt { get; set; }
public bool aBool { get; set; }
public BasicStuff theBasic { get; set; }
public BasicStuff[] theBasicArray { get; set; }
}
So what I came up with was the following:
public static bool AllPublicPropertiesEqual<T>(T AObj, T BObj, params string[] ignore) where T : class
{
if (AObj != null && BObj != null)
{
Type type = typeof(T);
List<string> ignoreList = new List<string>(ignore);
foreach (PropertyInfo pInfo in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (!ignoreList.Contains(pInfo.Name))
{
if (pInfo.PropertyType.IsArray)
{
object AValue = type.GetProperty(pInfo.Name).GetValue(AObj, null);
object BValue = type.GetProperty(pInfo.Name).GetValue(BObj, null);
string t = AValue.GetType().ToString();
if (!AllPublicPropertiesEqual<object>(AValue, BValue))
return false;
}
else if (!pInfo.PropertyType.IsValueType && !pInfo.PropertyType.IsPrimitive && !pInfo.PropertyType.IsEnum && pInfo.PropertyType != typeof(string))
//else if (Convert.GetTypeCode(pInfo.PropertyType) == TypeCode.Object)
{
object AValue = type.GetProperty(pInfo.Name).GetValue(AObj, null);
object BValue = type.GetProperty(pInfo.Name).GetValue(BObj, null);
if (!AllPublicPropertiesEqual<object>(BValue, AValue))
return false;
}
else
{
object selfValue = type.GetProperty(pInfo.Name).GetValue(AObj, null);
object toValue = type.GetProperty(pInfo.Name).GetValue(BObj, null);
if (selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue)))
return false;
}
}
}
return true;
}
return AObj == BObj;
}
Only this fails because when recursively calling AllPublicPropertiesEqual, I need to pass the specific values type rather than just a generic object type.
But I dont know how to do this.... Any ideas are greatly appreciated...
Your method does not have to be generic. You can change the beginning of the method to:
public static bool AllPublicPropertiesEqual(object AObj, object BObj, params string[] ignore)
{
if (AObj != null && BObj != null)
{
Type type = AObj.GetType();
if (BObj.GetType() != type)
throw new Exception("Objects should be of the same type");
....
}
....
}
Well first thing what is that you should create Interface for those classes because from what I see you can combine those classes but all of them will have the same properties. Second option is create some Base abstract class with those properties and inherit from it. It is up to you what you choose.
Than create in this Base class or Interface Equals functions where you will equal directly the shared properties it is much easier and I think more efficient :-)
public abstract class BaseStaff {
public int anInt { get; set; }
public bool aBool { get; set; }
public abstract bool Equals(BaseStaff anotherStaff);
}
Then you can use this class and create your BasicStaff class and AllTheStaff class too in BasicStaff implement Equals method like this.
public override bool Equals(BaseStaff staff) {
this.anInt == staff.anInt &&
this.aBool == staff.aBool;
}
In AllTheStaff u can than override this method like this
public override bool Equals(BaseStaff staff) {
bool baseEquals = base.Equals(staff);
bool basicStaffEquals = this.BasicStaff.Equals(staff);
return baseEquals || basicStaffEquals;
}
This is just core idea and maybe I dont understand you well what you really want to achieve but hope it helps you :)
tested Solution
public static bool AllPublicPropertiesEqual<T>(T AObj, T BObj, params string[] ignore) where T : class
{
if (AObj != null && BObj != null)
{
Type type = typeof(T);
List<string> ignoreList = new List<string>(ignore);
foreach (PropertyInfo pInfo in type.GetCType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (!ignoreList.Contains(pInfo.Name))
{
if (pInfo.PropertyType.IsArray)
{
object aElementValues = (type.GetProperty(pInfo.Name).GetValue(AObj, null));
object bElementValues = (type.GetProperty(pInfo.Name).GetValue(BObj, null));
if (aElementValues != null && bElementValues != null)
{
List<object> AListValues = new List<object>();
List<object> BListValues = new List<object>();
foreach (var v in (IEnumerable)aElementValues)
AListValues.Add(v);
foreach (var v in (IEnumerable)bElementValues)
BListValues.Add(v);
if (AListValues.Count == BListValues.Count)
{
object[] aArray = AListValues.ToArray();
object[] bArray = BListValues.ToArray();
for (int i = 0; i < aArray.Length; i++)
{
if (!AllPublicPropertiesEqual(aArray[i], bArray[i]))
return false;
}
}
else
return false;
}
else if ((aElementValues == null) != (bElementValues == null))
return false;
}
else if (!pInfo.PropertyType.IsValueType && !pInfo.PropertyType.IsPrimitive && !pInfo.PropertyType.IsEnum && pInfo.PropertyType != typeof(string))
//else if (Convert.GetTypeCode(pInfo.PropertyType) == TypeCode.Object)
{
object AObjectValue = type.GetProperty(pInfo.Name).GetValue(AObj, null);
object BObjectValue = type.GetProperty(pInfo.Name).GetValue(BObj, null);
if (!AllPublicPropertiesEqual(BObjectValue, AObjectValue))
return false;
}
else
{
object aValue = type.GetProperty(pInfo.Name).GetValue(AObj, null);
object bValue = type.GetProperty(pInfo.Name).GetValue(BObj, null);
if (aValue != bValue && (aValue == null || !aValue.Equals(bValue)))
return false;
}
}
}
return true;
}
return AObj == BObj;
}
}

C# Generic Objects Compareing

This is generic class to compare two objects with a user defined property, but it doesn't work correctly.
public class ObjectComparer<T> : IEqualityComparer<T>
{
private string _propertyName;
private string PropertyName
{
get { return _propertyName; }
set
{
_propertyName = value;
PropertyInfo = typeof(T).GetProperty(PropertyName);
}
}
public ObjectComparer(string propertyName)
{
PropertyName = propertyName;
}
private PropertyInfo PropertyInfo { get; set; }
public int GetHashCode(T type)
{
if (type== null)
{
return 0;
}
return PropertyInfo.GetHashCode();
}
public bool Equals(T obj1, T obj2)
{
if (ReferenceEquals(obj1, obj2))
{
return true;
}
if (ReferenceEquals(obj1, null) || ReferenceEquals(obj2, null))
{
return false;
}
return PropertyInfo.GetValue(obj1, null) == PropertyInfo.GetValue(obj2, null);
}
}
Issue 1)type == null (Possible compare of value type with null)
Issue 2)Use ReferenceEquals in this position is correct?
Update
"Word" class:
public class Word
{
public string EnglishText { get; set; }
public string LocalText { get; set; }
public string EditedText { get; set; }
}
Usage :
var except = Program.Localizer.DefaultLanguage.Words.Except(CurrentSelectedLanguage.Words, new ObjectComparer<Word>("EnglishText"));
Number of different objects based on the "EnglishName" is not correct.
Class authentic and Modified below.
Special thanks to Sriram Sakthivel
public class ObjectComparer<T> : IEqualityComparer<T>
{
private string _propertyName;
private string PropertyName
{
get { return _propertyName; }
set
{
_propertyName = value;
PropertyInfo = typeof(T).GetProperty(PropertyName);
}
}
public ObjectComparer(string propertyName)
{
PropertyName = propertyName;
}
private PropertyInfo PropertyInfo { get; set; }
public int GetHashCode(T obj)
{
if (ReferenceEquals(obj, null))
{
return 0;
}
return PropertyInfo.GetHashCode();
}
public bool Equals(T obj1, T obj2)
{
if (ReferenceEquals(obj1, obj2))
{
return true;
}
if (ReferenceEquals(obj1, null) || ReferenceEquals(obj2, null))
{
return false;
}
return Equals(PropertyInfo.GetValue(obj1, null),
PropertyInfo.GetValue(obj2, null));
}
}
Your GetHashCode is wrong. You're calling PropertyInfo.GetHashCode It should be using the Property value instead.
public int GetHashCode(T obj)
{
if (object.ReferenceEquals(obj, null))
{
return 0;
}
var value = PropertyInfo.GetValue(obj);
return value == null ? 0 : value.GetHashCode();
}
Also you can get rid of Possible compare of value type with null warning from resharper using object.ReferenceEquals
GetHashCode parameter is named as type, which is misleading, So I renamed it to obj
Update:
If you need to use value comparison you must use Object.Equals method as opposed to using == operator for object type
public bool Equals(T obj1, T obj2)
{
if (ReferenceEquals(obj1, obj2))
{
return true;
}
if (ReferenceEquals(obj1, null) || ReferenceEquals(obj2, null))
{
return false;
}
return object.Equals(PropertyInfo.GetValue(obj1, null),
PropertyInfo.GetValue(obj2, null));
}

Categories

Resources