Reputation: 23169
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
Reputation: 888177
Use the Guid
struct, like this.
public class Foo
{
public readonly Guid UniqueIdentifier;
public Foo()
{
UniqueIdentifier = Guid.NewGuid();
}
}
Upvotes: 0
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
Reputation: 17804
System.Guid guid = System.Guid.NewGuid();
String id = guid.ToString();
Upvotes: 17