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
?
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
?
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)
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.