divinci
divinci

Reputation: 23169

What is the simplest unique identifier available in .Net?

So I have this

public class Foo
{
    public int UniqueIdentifier;

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

How do I get a completely unique number?

Thanks!

Upvotes: 3

Views: 403

Answers (3)

SLaks
SLaks

Reputation: 888177

Use the Guid struct, like this.

public class Foo
{
    public readonly Guid UniqueIdentifier;

    public Foo()
    {
        UniqueIdentifier = Guid.NewGuid();
    }    
}

Upvotes: 0

another average joe
another average joe

Reputation: 668

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.

Upvotes: 2

Ropstah
Ropstah

Reputation: 17804

System.Guid  guid = System.Guid.NewGuid();
String id = guid.ToString();

Upvotes: 17

Related Questions