For reference types, you can get the same behaviour by not overriding GetHashCode, or by calling the GetHashCode method defined on System.Object; this will always return the same number for the same instance of an object.
If you've got another class in the way which overrides GetHashCode incorrectly, you can always call System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode  instead.
Creating a static int as the hashcode generator and, during the construction calling hahscode = Interlocked.Increment(ref _hashCodeGenerator) will solve the problem, will be thread safe and, at least during the execution time, will completely avoid duplicate values.
But, to be honest, I think the real source of the problem is the way you are working with your classes.
If the data is required for the database, then you should determine its (unchangeable) primary key before putting the object into a list. Note that it is usual to put some changeable properties as the primary key when they are small (like state names) but even rare, such things can change, so it is always better to have another unchangeable value.
Last Visit: 31-Dec-99 18:00 Last Update: 12-Aug-22 16:47