 <%@ Page language="c#"%> 
<script language="c#" runat=server>

class Singleton
{
    // Variable
    public int x= 0; 

    // Fields
    private static Singleton instance;

    // Empty Constructor
    protected Singleton() {}

    // Methods
    public static Singleton Instance()
    {
        // Uses "Lazy initialization"
        if( instance == null )
            instance = new Singleton();

        return instance;
    }
}

private void Page_Load(object sender, System.EventArgs e)
{
    
    // Constructor is protected
    // Prevented from using 'new' keyword
    Singleton s1 = Singleton.Instance();
    Singleton s2 = Singleton.Instance();

    if( s1 == s2 )
        Response.Write( "s1 & s2 are the same instance" );

    s1.x= 100;
    Response.Write("<br>s1.x=" + s1.x);
    
    s2.x= 200;
    Response.Write("<br>s2.x=" + s2.x);

    // Updates to x are updating same instance
    Response.Write("<br>s1.x=" + s1.x);

}


</script>
<html>
    <head>
        <title>Singleton</title>
    </head>
    <body>
    </body>
</html>
