问题
One field of our struct is Guid type. How to generate a valid value for it?
回答1:
Guid id = Guid.NewGuid();
回答2:
Guid.NewGuid()
creates a new random guid.
回答3:
There are two ways
var guid = Guid.NewGuid();
or
var guid = Guid.NewGuid().ToString();
both use the Guid class, the first creates a Guid Object, the second a Guid string.
回答4:
Guid.NewGuid() will create one
回答5:
var guid = new Guid();
Hey, its a 'valid', although not very useful, Guid.
(the guid is all zeros, if you don't know. Sometimes this is needed to indicate no guid, in cases where you don't want to use a nullable Guid)
回答6:
To makes an "empty" all-0 guid like 00000000-0000-0000-0000-000000000000
.
var makeAllZeroGuID = new System.Guid();
or
var makeAllZeroGuID = System.Guid.Empty;
To makes an actual guid with a unique value, what you probably want.
var uniqueGuID = System.Guid.NewGuid();
回答7:
System.Guid desiredGuid = System.Guid.NewGuid();
回答8:
If you want to create a "desired" Guid you can do
var tempGuid = Guid.Parse("<guidValue>");
where <guidValue>
would be something like 1A3B944E-3632-467B-A53A-206305310BAE
.
回答9:
If you are using this in the Reflection C#, you can get the guid from the property attribute as follows
var propertyAttributes= property.GetCustomAttributes();
foreach(var attribute in propertyAttributes)
{
var myguid= Guid.Parse(attribute.Id.ToString());
}
回答10:
//Retrive your key ID on the bases of GUID
declare @ID as uniqueidentifier
SET @ID=NEWID()
insert into Sector(Sector,CID)
Values ('Diry7',@ID)
select SECTORID from sector where CID=@ID
来源:https://stackoverflow.com/questions/2344098/c-sharp-how-to-create-a-guid-value