Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This turned out more interessting than I thought initially:</p> <p>So the best solution would be</p> <pre><code>public sealed class Singleton { private static readonly Singletion _instance = new Singleton(); private Singleton() { //do your construction } public static Singleton getInstance() { return _instance; } } </code></pre> <p>From my current understanding the programming environment (Java, .NET) should not matter for this solution.</p> <p>Any thoughts or comments?</p> <p>Further reading I have dug out:</p> <ul> <li>MSDN on Singletons:<a href="http://msdn.microsoft.com/de-de/library/ms998558.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/de-de/library/ms998558.aspx</a></li> <li>MSDN on static members: <a href="http://msdn.microsoft.com/en-us/library/79b3xss3.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/79b3xss3.aspx</a> Especially referring to <blockquote> <p>Static members are initialized before the static member is accessed for the first time and before the static constructor, if there is one, is called.</p> </blockquote></li> </ul> <p>Edit: As for Java it should also work:</p> <ul> <li>But it will not be lazy, the Singleton will be constructed once the class is loaded</li> <li>See also: Section 8.3.1.1 of <a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html" rel="nofollow noreferrer">http://java.sun.com/docs/books/jls/third_edition/html/classes.html</a></li> <li>See <a href="http://java.sun.com/docs/books/jls/third_edition/html/execution.html#44557" rel="nofollow noreferrer">http://java.sun.com/docs/books/jls/third_edition/html/execution.html#44557</a></li> </ul> <p>Now if someone points out a save C++ version it would be complete... (I away from C++ too long to remember the details...)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload