-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: SitaramanM
Message 14 in Discussion

Hi   A few more additions to this thread. I had gone thru this article pasted by 
naveen, sometime back and was quite happy ( :) )to find that,    whereas in Java even 
if you implement Singleton with proper Double Check Locking, the Java Compiler might 
mess up with Lazy 
Initialisation(http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html),
 in the case of .Net the Framework itself takes care of these messy internals...(one 
more ammo to use in those Java v/s .Net superiority battles :) )   Also note that the 
syntax that u had given for writing a singleton (changed to VB.Net)   Public Class 
Singleton1
    Public Shared ReadOnly Instance As Singleton1 = New Singleton1()
    Private Sub New()
        'Constructor Explicitly Made as Private Will Ensure that someone does not 
Inadvertantly use new to create an object of 
        ' this type.  
    End Sub End Class can also be written as   Public Class Singleton
    Public Shared ReadOnly Instance As Singleton     Private Sub New()
        'Constructor Explicitly Made as Private Will Ensure that someone does not 
Inadvertantly use new to create an object of 
        ' this type.  
    End Sub     Shared Sub New()
        ' This is the Type Initializer.  This will be converted by the compiler to the 
.cctor function
        Instance = New Singleton()
    End Sub End Class The Shared Sub New is basically a type initializer expression.   
This function will be converted to a .cctor method by the compiler when the source is 
compiled(just like the constructor is converted to a .ctor method).You can check this 
using the ILDASM tool.      However, do note that even if you do not specify the 
Shared Sub New (as done in the original code of yours),   the compiler will still 
create a .cctor method and move the initialization code of any shared (Shared in 
VB.Net and Static in C#) member variables into this .cctor function.    So ultimately, 
the compiled IL Code of both the snippets above will be exactly the same !!!!   That 
said, the difference in the method provide by you and the modified method provided by 
me is that you are using a "Field Initializer expression" and i used a Type 
Initializer Expression.  They have the same behaviour,  and the only difference tht i 
could see was that when using a explicit type initializer function(Shared Sub New ), 
you can control the order of the initialization, whereas if you use Field Initializer 
Expression,  the compiler is the boss in this regard.   Thought it would be worthwile 
to paste this snippet from Don Box's .Net Internals Book    Types are allowed to 
provide a distinguished method that is called when the type is first initialized. This 
type initializer is simply a static method with a well-known name (.cctor). A type can 
have at most one type initializer, and it must take no parameters and return no value. 
Type initializers cannot be called directly; rather, they are called automatically by 
the CLR as part of the type's initialization. Each programming language provides its 
own syntax for defining a type initializer. In VB.NET, you simply write a Shared (per 
type) subroutine named New. In C#, you must write a static method whose name is the 
same as the declaring type name but has no return type. The following shows a type 
initializer in C#:namespace AcmeCorp.LOB {   public sealed class Customer {     
internal static long t;     static Customer() { // this is the type initializer!       
t = System.DateTime.Now.Ticks;     }   } }  
This code is semantically equivalent to the following type definition, which uses a C# 
field initializer expression rather than an explicit type initializer:namespace 
AcmeCorp.LOB {   public sealed class Customer {     internal static long t = 
System.DateTime.Now.Ticks;   } } Given this type definition, the fields will be 
initialized in the following order: t2, t3, t1.      hth   regards,   sr

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to