Tutorial :What is the simplest unique identifier available in .Net?



Question:

So I have this

public class Foo  {      public int UniqueIdentifier;        public Foo()      {          UniqueIdentifier = ????      }      }  

How do I get a completely unique number?

Thanks!


Solution:1

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


Solution:2

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.


Solution:3

Use the Guid struct, like this.

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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »