How to declare a constant Guid in C#?

C#ConstantsGuidCompile Time-Constant

C# Problem Overview


Is it possible to declare a constant Guid in C#?

I understand that I can declare a static readonly Guid, but is there a syntax that allows me to write const Guid?

C# Solutions


Solution 1 - C#

No. The const modifier only applies to "primitive" types (bool, int, float, double, long, decimal, short, byte) and strings. Basically anything you can declare as a literal.

Solution 2 - C#

Declare it as static readonly Guid rather than const Guid

Solution 3 - C#

public static readonly Guid Users = new Guid("5C60F693-BEF5-E011-A485-80EE7300C695");

and that's that.

Solution 4 - C#

While you can't seem to do that you can do that to be parsed whenever you need it:

const string _myGuidStr = "e6b86ea3-6479-48a2-b8d4-54bd6cbbdbc5";

But don't use the above guid as it belongs to me solely, I first generated it so I claim ownership on this particular guid above! But I'm generious - use this one instead (I don't like how it talks back to me but it's a nice guid overall when it keeps its mouth shut): 284c694d-d9cc-446b-9701-b391876c8394

Solution 5 - C#

I am doing it like this:

public static class RecordTypeIds
{
    public const string USERS_TYPEID = "5C60F693-BEF5-E011-A485-80EE7300C695";
    public static Guid Users { get { return new Guid(EntityTypeIds.USERS_TYPEID); } }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsmartcavemanView Question on Stackoverflow
Solution 1 - C#Quick Joe SmithView Answer on Stackoverflow
Solution 2 - C#Jaime BoteroView Answer on Stackoverflow
Solution 3 - C#StefanView Answer on Stackoverflow
Solution 4 - C#walt jimiView Answer on Stackoverflow
Solution 5 - C#ProVegaView Answer on Stackoverflow