80

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?

smartcaveman
  • 41,281
  • 29
  • 127
  • 212
  • 7
    What's wrong with a static readonly Guid? – Pedro Feb 07 '11 at 21:07
  • 2
    Nothing, if it's the only way to do it. – smartcaveman Feb 07 '11 at 21:08
  • 3
    @Pedro, a const is evaluated at compile-time, a static is evaluated at run-time. – Doug Domeny Apr 05 '13 at 14:14
  • 1
    pretty close: [why-cant-structs-be-declared-as-const](http://stackoverflow.com/questions/4590796/why-cant-structs-be-declared-as-const) – nawfal Dec 13 '13 at 19:48
  • As some of the answers point out there is currently no way to make a Guid const. ProVega's answer using a const string to me is probably best answer. I don't like his property usage but I would create a static readonly Guid based on the string. Main advantage is you can use Guid's normally with the one exception being a switch statement. At that point you could use the const string member. switch statement is only occasion that I've ran into where static readonly doesn't work. – Dan P Feb 27 '17 at 16:38

6 Answers6

79

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.

Quick Joe Smith
  • 8,074
  • 3
  • 29
  • 33
  • 11
    It's actually defined the other way around. You can use `const` for any compile time literal values. C# doesn't actually define the term "primitive type"; it's technically just slang. Other languages use actually define the term "primitive types" and have a fixed list of such types. In fact, there is a single compile time literal for `Guid`, and that is `default(Guid)`. That is the only compile time literal for Guid though. – Servy Oct 25 '13 at 14:51
57

Declare it as static readonly Guid rather than const Guid

Jaime Botero
  • 2,263
  • 2
  • 22
  • 14
  • I believe this is the best answer because it is the only one that provides an alternative without changing the final result, the static readonly can be used in the same way of a const, including intellisense – Raffaeu May 03 '16 at 08:50
  • 8
    @Raffaeu: It's a very useful posting (+1 from me), but technically, it does not even answer the question ("Is it possible to declare a constant Guid in C#?"), nor does it give any explanation on *why* the answer is yes or no. It provides a workaround, but does not explain how `static readonly` differs from `const`, and what the implication are of using one instead of the other. Thus, it's far from what a "best answer" should look like. – Heinzi Mar 15 '17 at 14:07
43
public static readonly Guid Users = new Guid("5C60F693-BEF5-E011-A485-80EE7300C695");

and that's that.

Stefan
  • 473
  • 4
  • 2
  • 3
    It is however a suitable alternative. – SWalters Dec 19 '13 at 17:29
  • 11
    It's not a suitable alternative if you need something that is evaluated at compile time. – Niels Brinch Jul 15 '14 at 08:20
  • A usecase of this not being constant would be **START CODE** public static readonly Guid defaultGuid = new Guid("5C60F693-BEF5-E011-A485-80EE7300C695"); protected bool (Guid userID = defaultGuid); { return false; } **END CODE** wouldn't compile. as the guid isn't a compile time constant – Liam Laverty Jun 18 '15 at 10:27
  • Exactly what I was looking for ! :) – napi15 Sep 24 '15 at 15:27
22

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

walt jimi
  • 262
  • 1
  • 3
7

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); } }
}
ProVega
  • 5,864
  • 2
  • 36
  • 34
  • 12
    you'll get marginally better performance with a readonly field instead of a property that creates a new Guid for every access – smartcaveman Jul 25 '12 at 20:30
  • 2
    I like your answer the best minus the get property. I agree with smartcaveman...just make that a public static readonly Guid. – Dan P Feb 27 '17 at 16:39
  • 1
    Ohh and only reason I like this the best is that it allows the string to be used in switch statement. All other places I use the static readonly Guid. – Dan P Feb 27 '17 at 16:40
0

Depends on what you want a const for.

If you want to have a known GUID that you can reuse as needed, the above solution of storing the GUID as a string and parsing to GUID as needed works.

If you need a default parameter value, that won't work. The alternative is to use a nullable Guid and use null as your constant value.

public void Foo(string aParameter, Guid? anOptionalGuid = null)
Denise Skidmore
  • 2,286
  • 22
  • 51