Reason for my vote of 2
you say "correctly" means make a random number - no correct for you problem is: not ovverride the hashcode at all! therefore the bad vote...
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 [1] 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 19:00 Last Update: 2-Dec-23 20:45