C# how to create a Guid value?

0 votes
asked Feb 26, 2010 by 5yrslaterdba

One field of our struct is Guid type. How to generate a valid value for it?

9 Answers

0 votes
answered Jan 26, 2010 by adam-driscoll

Guid.NewGuid() creates a new random guid.

0 votes
answered Jan 26, 2010 by dana-holt

Guid.NewGuid() will create one

0 votes
answered Jan 26, 2010 by justin

There are two ways

Guid guid = Guid.NewGuid();

or

String guid = Guid.NewGuid().ToString();

both use the Guid class, the first creates a Guid Object, the second a Guid string.

0 votes
answered Jan 26, 2010 by will
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)

0 votes
answered Feb 26, 2010 by david
Guid id = Guid.NewGuid();
0 votes
answered Jan 6, 2012 by siphab
System.Guid desiredGuid = System.Guid.NewGuid();
0 votes
answered Jan 6, 2015 by reza-cse08

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(); 
0 votes
answered Jan 7, 2016 by eldon-elledge

SHAMELESS COPY OF ANSWER ABOVE

Guid had a built in method for getting a Guid value. Try the line below.

var id = Guid.NewGuid();
0 votes
answered Sep 15, 2017 by sammer
//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
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...