Declaring variables - best practices
.net, c#
Solution
If you set your variable outside of the constructor then there is no error handling (handeling) available. While in your example it makes no difference, but there are many cases that you may want to have some sort of error handling. In that case using your first option would be correct.
Nescio talked about what implication this would have on your applicaiton if there were some constructor failures.
For that reason, I always use Option #1.
Problem
I found a while ago (and I want to confirm again) that if you declare a class level variable, you should not call its constructor until the class constructor or load has been called. The reason was performance - but are there other reasons to do or not do this? Are there exceptions to this rule? ie: this is what I do based on what I think the best practice is: ``` public class SomeClass { private PersonObject _person; public SomeClass() { _person = new PersonObject("Smitface"); } } ``` opposed to: ``` public class SomeClass { private PersonObject _person = new PersonObject("Smitface"); public SomeClass() { } } ```