Search code examples
c#readonly-attribute

Is there a difference between readonly and { get; }


Do these statements mean the same thing?

int x { get; }
readonly int x;

Solution

  • In answer to your question: There is a difference between readonly and {get; }:

    In int x { get; } (which won't compile as there's no way to set x - I think you needed public int x { get; private set; } ) your code can keep changing x

    In readonly int x;, x is initialised either in a constructor or inline and then can never change.