What is the best way of initializing objects for properties without setters in C#?
For example I have property of type UserData and I can initialize it:
- In constructor
In getter
private UserData _user; public UserData User { get { return _user?? (_user= new UserData ()); } }
Initialize field:
private UserData _user = new UserData()
I found few similiar threads:
Create an object in the constructor or at top of the class
C# member variable initialization; best practice?
But it is consideration between 1st and 3rd option - no one thinks about 2nd option - do you know way? From some time it is my preffered option to get objects, but I wonder if there are some cons that I don't know.
Could you tell me what is the best option and what problem could make use of 2nd option?