views:

271

answers:

3

So I have this

public class Foo
{
    public int UniqueIdentifier;

    public Foo()
    {
        UniqueIdentifier = ????
    }    
}

How do I get a completely unique number?

Thanks!

+17  A: 
System.Guid  guid = System.Guid.NewGuid();
String id = guid.ToString();
Ropstah
There are also some parameters you can pass to guid.ToString() to affect the sort of string you get back.
John Fisher
+2  A: 

Although not an int, a method of creating unique identifiers commonly uses GUIDs. You can use Guid.NewGuid() to generate one.

There are some various conversion methods including byte arrays and strings. For more information on GUIDs you can read up on them at Wikipedia.

Best of luck.

another average joe
A: 

Use the Guid struct, like this.

public class Foo
{
    public readonly Guid UniqueIdentifier;

    public Foo()
    {
        UniqueIdentifier = Guid.NewGuid();
    }    
}
SLaks
HashCode is merely a hash for an object and is NOT always unique. Sometimes it's a mem address, sometimes it's other things. But point being, it's never guaranteed unique, even with a given CLR or Java VM.
TheSoftwareJedi
You're right; I failed to see that.Removed
SLaks