static vs const

static vs const

@csharp_1001_notes


There are situations where a const and a non-const have different semantics. For example:

const int y = 42;

static void Main()
{
  short x = 42;
  Console.WriteLine(x.Equals(y));
}

prints out True, whereas:

static readonly int y = 42;

static void Main()
{
  short x = 42;
  Console.WriteLine(x.Equals(y));
}

writes False.

Do you know why? No? Here we go: https://stackoverflow.com/a/18744539/2524304

Report Page