Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Regarding question (1): The answer from Jon is correct, since he implicitly marks the class 'Nested' private by not making it public or internal :-). You might as well do it explicitly by adding 'private':</p> <pre><code> private class Nested </code></pre> <p>Regarding question (2): basically what <a href="http://csharpindepth.com/Articles/General/Beforefieldinit.aspx">the post about beforeinitfield</a> and <a href="http://codeblog.jonskeet.uk/2010/01/26/type-initialization-changes-in-net-4-0/">type initialization</a> tell you is that if you have no static constructor, the runtime can initialize it at any time (but before you use it). If you do have a static constructor, your code in the static constructor might initialize the fields, which means that the runtime is only allowed to initialize the field when you ask for the type.</p> <p>So if you don't want the runtime to initialize fields 'proactively' before you use them, add a static constructor.</p> <p>Either way, if you're implementing singletons you either want it to initialize as lazy as possible and not when the runtime thinks it should initialize your variable -- or you probably just don't care. From your question I suppose you want them as late as possible.</p> <p>That brings met to Jon's post about <a href="http://csharpindepth.com/Articles/General/Singleton.aspx">singleton</a>'s, which is IMO the underlying topic of this question. Oh and the doubts :-)</p> <p>I'd like to point out that his singleton #3, which he marked 'wrong', is actually correct (because lock's automatically <a href="http://www.albahari.com/threading/part4.aspx">implies a memory barrier on exit</a>). It also should be faster than singleton #2 when you use the instance more than once (which is more or less the point of a singleton :-) ). So, if you really need a lazy singleton implementation, I'd probably go for that one - for the simple reasons that (1) it's very clear for everyone that reads your code what is going on and (2) you know what will happen with exceptions.</p> <p>In case you're wondering: I would never use singleton #6 because it can easily lead to deadlocks and unexpected behavior with exceptions. For details, see: <a href="http://msdn.microsoft.com/en-us/library/system.threading.lazythreadsafetymode.aspx">lazy's locking mode</a>, specifically ExecutionAndPublication.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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