How to use guids in c #?

This code:

Something = new Guid() 

returns:

00000000-0000-0000-0000-000000000000

all the time, and I can’t say why? So why?

+85
c # guid
May 24 '09 at 15:43
source share
5 answers

You must use Guid.NewGuid()

+145
May 24 '09 at 15:46
source share

Just explain why you need to call NewGuid and not use the default constructor ... In .NET, all structures (value types such as int, decimal, Guid, DateTime, etc.) must have a constructor without parameters without parameters, which initializes all fields by default. In the case of Guid, the bytes that make up Guid are zero. Instead of making a special case for Guid or making it a class, they use the NewGuid method to generate a new β€œrandom” guide.

+60
May 24 '09 at 15:51
source share

In System.Guid.

To dynamically create a GUID in code:

 Guid messageId = System.Guid.NewGuid(); 

To see its meaning:

 string x = messageId.ToString(); 
+12
May 24 '09 at 16:01
source share
  Guid g1 = Guid.NewGuid(); string s1; s1 = g1.ToString(); Console.WriteLine("{0}",s1); Console.ReadKey(); 
+3
Sep 03 '12 at 7:13
source share

something = new Guid() is equal to something = Guid.Empty .

Use Guid.NewGuid(); instead

+3
Sep 26 '12 at 13:57
source share



All Articles