Which one is better to have auto-implemented property with private setter or private field and property just getter?

Posted by PLB on Stack Overflow See other posts from Stack Overflow or by PLB
Published on 2012-06-29T09:11:31Z Indexed on 2012/06/29 9:15 UTC
Read the original article Hit count: 160

Filed under:

My question may be a part of an old topic - "properties vs fields".

I have situation where variable is read-only for outside class but needs to modified inside a class. I can approach it in 2 ways:

First:

private Type m_Field;
public Type MyProperty { get { return m_Field; } }

Second:

public Type MyProperty { get; private set; }

After reading several articles (that mostly covered benefits of using public properties instead of public fields) I did not get idea if the second method has some advantage over the first one but writing less code. I am interested which one will be better practice to use in projects (and why) or it's just a personal choice.

Maybe this question does not belong to SO so I apologize in advance.

© Stack Overflow or respective owner

Related posts about c#