How to use Guids in C#?

删除回忆录丶 提交于 2019-12-20 08:35:08

问题


This Code:

Something = new Guid() 

is returning:

00000000-0000-0000-0000-000000000000

all the time and I can't tell why? So, why?


回答1:


You should use Guid.NewGuid()




回答2:


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.




回答3:


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();



回答4:


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

Use Guid.NewGuid(); instead




回答5:


 Guid g1 = Guid.NewGuid();

 string s1;
 s1 = g1.ToString();
 Console.WriteLine("{0}",s1);
 Console.ReadKey();


来源:https://stackoverflow.com/questions/904021/how-to-use-guids-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!