I had a look at the following question:
Good GetHashCode() override for List of Foo objects respecting the order
And asked Jon Skeet the following: Good GetHashCode() override for List of Foo objects respecting the order
"@JonSkeet does that solution produce consistent result with List of string across multiple AppDomains? I have a WCF Server where multiple application from various platforms (XP, Vista, Win7 or different .Net Frameworks 3.5 and higher) can connect and in all those situations I need a consistent hashcode from a list of strings is that the case? If not how would I achieve that?"
His answer: "@RandRandom: You shouldn't be using GetHashCode for that - it's not meant to be consistent across different processes. You should use something like SHA-256 for that. "
Not sure how the implementation of his proposed answer would look like, so to not ask detailed question in the comments area I decided to create a new question.
So assume you have the following list:
var fooList = new List<string>()
{
"",
"StatAccount",
"Value",
"9900000701",
"P1",
"3",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"VFE-Lage.xlsx",
"",
"",
"False",
"",
"",
"",
"",
"",
"",
"",
"",
};
Now I need a hashCode that's easier/faster to compare than checking for SequenceEquality, my list is immutable it never changes.
I have a class looking something like this:
public class Foo
{
private List<string> _fooList = new List<string>();
private int _fooListHashCode;
public List<string> FooList
{
get
{
return _fooList;
}
set
{
_fooList = value;
_fooListHashCode = GetListsHashCode(value);
}
}
public static int GetListsHashCode(List<string> list)
{
//return hashCode...
return 0;
}
public override int GetHashCode()
{
return _fooListHashCode;
}
public override bool Equals(object obj)
{
var foo = obj as Foo;
if (foo == null)
return false;
return this._fooListHashCode == foo._fooListHashCode;
}
}