I have a question concerning the Guid as a method to generate IDs. I need to be able to generate a new unique ID for each instance of a given class (I expect there to be a couple of hundreds of instances). I am including my code below:
class Program
{
static void Main(string[] args)
{
Instance instance1 = new Instance();
Console.WriteLine(instance1.ID);
Instance instance2 = new Instance();
Console.WriteLine(instance2.ID);
Console.ReadKey();
}
}
public static class GenerateID
{
public static string NewID()
{
Guid guid = Guid.NewGuid();
string id = guid.ToString();
return id;
}
}
public class Instance
{
public string ID = GenerateID.NewID();
}
From what I can see, this method works, and two unique IDs are generated each time when I run the code, but I am not able to test this extensively, since I read that Guid is able to generate like a badgilion of IDs.
Would the way I implemented the above ensure that the ID's are unique each time? Or is there any chance of a collision? If so, what would be a better way to go about assigning unique IDs to instances?
EDIT: Alright, so I learned from ppl's answers that Guid is not inherently 100% unique (thanks, I'll make sure to read up on that), but my question is whether the method I used to get the ID may heighten the chance that the same two IDs will be generated? Or is this a good way to go about obtaining unique IDs?