How to use Guids in C#?

后端 未结 5 1584
名媛妹妹
名媛妹妹 2021-01-30 19:38

This Code:

Something = new Guid() 

is returning:

00000000-0000-0000-0000-000000000000

all the time

相关标签:
5条回答
  • 2021-01-30 19:45

    something = new Guid() equals something = Guid.Empty.

    Use Guid.NewGuid(); instead

    0 讨论(0)
  • 2021-01-30 19:47

    You should use Guid.NewGuid()

    0 讨论(0)
  • 2021-01-30 19:54

    It's in System.Guid.

    To dynamically create a GUID in code:

    Guid messageId = System.Guid.NewGuid();
    

    To see its value:

    string x = messageId.ToString();
    
    0 讨论(0)
  • 2021-01-30 20:01

    Just a quick explanation for why you need to call NewGuid as opposed to using the default constructor... In .NET all structures (value types like int, decimal, Guid, DateTime, etc) must have a default parameterless constructor that initializes all of the fields to their default value. In the case of Guid, the bytes that make up the Guid are all zero. Rather than making a special case for Guid or making it a class, they use the NewGuid method to generate a new "random" Guid.

    0 讨论(0)
  • 2021-01-30 20:04
     Guid g1 = Guid.NewGuid();
    
     string s1;
     s1 = g1.ToString();
     Console.WriteLine("{0}",s1);
     Console.ReadKey();
    
    0 讨论(0)
提交回复
热议问题