Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a GUID in C#

Tags:

guid

c#-4.0

I've got a lot of code that is using GUIDs (an architecture handed to me--not my choice).

Mostly, the values come from a database and load into memory from there. However, I'm doing some testing and I'm hard-coding some GUIDs into the code.

I haven't found an easy way to assign GUIDs, so I've ended up using Guid.Parse("...."). Is there an easier way to assign GUIDS in C#?

value = Guid.Parse("11223344-5566-7788-99AA-BBCCDDEEFF00"); 

It just seems like a lot of overhead to create the string then parse it. I would think there would be an easier way to directly assign it.

like image 776
Richard Avatar asked Jun 18 '13 12:06

Richard


People also ask

How do I assign a GUID?

In case you use the guid as a constant - you can put the guid in your project settings. Show activity on this post. If this is C# snippet then it will not compile because a const cannot be marked as static.

How is a GUID generated?

A GUID (globally unique identifier) is a 128-bit text string that represents an identification (ID). Organizations generate GUIDs when a unique reference number is needed to identify information on a computer or network. A GUID can be used to ID hardware, software, accounts, documents and other items.

What is GUID NewGuid () in C#?

Initializes a new instance of the Guid structure. public: static Guid NewGuid(); C# Copy.

Why GUID is used in C#?

A Globally Unique Identifier or GUID represents a gigantic identification number — a number so large that it is mathematically guaranteed to be unique not only in a single system like a database, but across multiple systems or distributed applications.


1 Answers

If you already have a string representation of the Guid, you can do this:

Guid g = new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"); 

And if you want a brand new Guid then just do

Guid g = Guid.NewGuid(); 
like image 192
Tom Chantler Avatar answered Sep 28 '22 19:09

Tom Chantler