I am trying to make an extension method in C# that takes in a collection of objects and can make a hash from it. The problem is I haven't been able to figure out a fast way of doing it. I am trying to have it be done in O(1) time but I haven't found any useful information out there. This is what I currently have but it is slow and doesn't really work (its just an example):
public static int GetCollectionHash(this IEnumerable collection)
{
HashCode hash = new();
foreach (var o in collection)
{
hash.Add(o);
}
return hash.ToHashCode();
}
the only way to do what you want is to create your own collection and overriding the add/remove/etc method and update a private variable that contain the hash when they are called
at that point, just read that new variable