My team follows the general .NET convention that you should expose public properties on your types rather than exposing fields. However, I've come across a class which mostly exposes fields... but then contains this for its two 'identifying' pieces of data:
public readonly int WidgetId;
public readonly string WidgetName;
I am really heavily tempted to refactor these into properties, i.e.
public int WidgetId { get; }
public string WidgetName { get; }
So they match the conventions of the rest of the class. I'm aware of the arguments for and against public fields versus properties. But, given that readonly-auto-properties in particular are a relatively new C# feature, I want to know:
What are the semantic differences between public readonly int Foo
and public int Foo { get; }
, if any? Are they meaningfully different to the compiler?