c# - Is implementing a singleton using an auto-property a good idea? -
I have recently found out about auto-property and they like a lot. At this time I am trying to use all the places where I can. Actually, not just able to use them everywhere, but to see how well they work in most situations.
Now I'm making a singleton and thought: "Hey, try here's auto-properties."
Public Class Mistlington {Receive public status maslingington mieslington; Private set; } Private Missington () {} Static MyStington () {MyScillton = New MyStyling (); }}
So my question is: "Is it good to implement a singleton?"
I'm not asking if a singleton is normal good idea.
I will not do this personally. I do not like to use automatically implemented properties with a private setter, which you do not ever call, where you want a readworthy property supported by only a readable variable to read. To be more clear about what you mean, there is only one more line of code:
Public sealed class MySstonington {Personal Static Readable Maisinglington MySellington; Public static MySinglington Mystington {mySingleton return}; }} Private Mistington () {} Static MyStington () {mySingleton = New MyStington (); }}
In this way, to change any temptation to the singleton class to reassign the property or variable, because the compiler will stop them.
In other words:
- They must be added to read a setter and / or variable, which is a major change - which is expected that they will reconsider.
- Yes, it will work.
- No, I do not think this is a good idea.
In the form of C # 6, this reading- only automatically implemented properties, however:
public sealed class MySingleton {public static MySingleton MySingleton {get ; } = New MyStington (); Private Mistington () {} Static MyStington () {}}
Comments
Post a Comment