SpiritBob
SpiritBob

Reputation: 2672

Why does Microsoft suggest we use properties for static data types, rather than static fields?

Is this still relevant?

It is recommended that you use static properties instead of public static fields whenever possible.

If so, why?

EDIT: I don't think it's an opinion based-question when we are asking for clarification on why Microsoft's coding style is defined the way it is.

Upvotes: 1

Views: 106

Answers (1)

René Vogt
René Vogt

Reputation: 43886

In the linked article, there is another link, Field Usage Guidelines, containing this explanation:

Do not use instance fields that are public or protected (Public or Protected in Visual Basic). If you avoid exposing fields directly to the developer, classes can be versioned more easily because a field cannot be changed to a property while maintaining binary compatibility.

That applies to static fields as well as to non-static/instance fields. It means that you can later change how the field backing a property maybe set or get without breaking dependencies.
If you released the class with a public field, you can never change this without troubling developers already accessing that field.

Upvotes: 5

Related Questions