anonim anonim - 5 months ago 25
C# Question

How generate unique Integers based on GUIDs

Is it possible to generate (highly probable) unique Integer from GUIDs?

int i = Guid.NewGuid().GetHashCode();

int j = BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0);

Which one is better?


Eric Lippert did a very interesting (as always) post about the probability of hash collisions.

You should read it all but he concluded with this very illustrative graphic:

Probability of hash collisions

Related to your specific question, I would also go with GetHashCode since collisions will be unavoidable either way.