C# how to create a Guid value?

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


Solution 1:

Guid id = Guid.NewGuid();

Solution 2:

Guid.NewGuid() creates a new random guid.

Solution 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.

Solution 4:

Guid.NewGuid() will create one

Solution 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)