0

I saw someone using like

private bool someVariable { get; set; } = false;

though it doesn't hurt running the code it appears to me not a good practice and in my own understanding it's an overkill as this is a private variable but I want other's good opinion on this.

I said so because looks like using like this

private bool someVariable = false;

is more appropriate and proves no wrong and proper. Am I right?

user3856437
  • 2,071
  • 4
  • 22
  • 27
  • I mean when you instantiate a non-nullable boolen then its default value is false anyway so in your example both are just code noise. Maybe your example would be better with a different type? – Travis Acton Sep 02 '20 at 14:34
  • The merit of private properties is discussed [here](https://stackoverflow.com/questions/3310186/are-there-any-reasons-to-use-private-properties-in-c). So no, this one doesn't make much sense. Maybe it got auto-generated by the author's IDE or add-in. It is not otherwise overkill, it behaves like a field in the Release build. – Hans Passant Sep 02 '20 at 14:35
  • 1
    `{ get; set; }` is just semantic sugar to tell the compiler to generate a getter and a setter, thus you can add logic when accessing a field or writing to a field. Neither is bad, it depends on the use case. Its like asking if `public / private` is better, it doesn't mean anything without context. – Kieran Devlin Sep 02 '20 at 14:37
  • I think it's bad only if there's really nothing to add on its logic of getter and setter. Since my point is same as using a regular variable being declared and initialized a simple `private bool someVariable = false;` will just be enough. That's why I said it an overkill. Thanks for the info, because of the inputs i was able to comprehend it. – user3856437 Sep 06 '20 at 23:54

0 Answers0