1

Out of curiosity, Is there a way to declare Guid.Empty, nay any Guid as const? I once thought all value types can be declared as a constant.

Line below returns the error: CS0283 The type 'Guid' cannot be declared const

const Guid myConstGuid = Guid.Empty;
Barak
  • 535
  • 6
  • 18
  • 1
    https://stackoverflow.com/questions/4926573/how-to-declare-a-constant-guid-in-c – Kurt Hamilton Feb 10 '20 at 07:20
  • Constant values should be "known" at compile time. – Fabio Feb 10 '20 at 07:20
  • [const (C# Reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/const): *Constants can be numbers, Boolean values, strings, or a null reference.* – dbc Feb 10 '20 at 07:21
  • Why you need a constant ?`Guid.Empty` already plays a role of readonly value. – Fabio Feb 10 '20 at 07:24

1 Answers1

1

The const modifier is only valid for primitive types.

You could however declare it as readonly.

Riscie
  • 3,775
  • 1
  • 24
  • 31