C# – How to generate Guid

.NET Framework provides built-in Guid structure which allows to generate unique identifier.

The usage is very simple and requires to call NewGuid function on Guid structure.

Returned object type is Guid however we can easily convert it to string.

Let’s print it to screen.

As the result we can see randomly generated identifier containing lowercase letters, numbers and hyphens.

Depending on the personal requirements we can convert the Guid to different form. Let’s say uppercase letters, numbers and no hyphens.

Above line generates new Guid, converts it to string, replaces hyphens with empty strings and makes it all uppercase.

Let’s print to console.