.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Implementing the Singleton Pattern in C#

Posted By: Sajid P K     Posted Date: October 21, 2010    Points: 2   Category :C#
The singleton pattern is one of the best-known patterns in software engineering. Essentially, a singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance. Most commonly, singletons don't allow any parameters to be specified when creating the instance - as otherwise a second request for an instance but with a different parameter could be problematic! (If the same instance should be accessed for all requests with the same parameter, the factory pattern is more appropriate.) This article deals only with the situation where no parameters are required. Typically a requirement of singletons is that they are created lazily - i.e. that the instance isn't created until it is first needed.

View Complete Post

More Related Resource Links

Singleton Design Pattern in C#, Vb.NET

Ensure a class has only one instance and provide a global point of access to it.

Implementing Continuous Scrolling UI Pattern in ASP.NET

When you have numerous records to show, we have to resort to Paging. There is a better alternative to display voluminous data (especially read only data) while providing a better user experience & also making efficient use of server bandwidth - the Continuous Scrolling or Infinite Scrolling UI Pattern. Read on to know more about how to implement Continuous Scrolling UI Pattern in ASP.NET with a GridView.

Singleton Design Pattern in Asp.net using C#

When we want to make a only one instance of a class and also making sure that there is a global access point to that object then the design pattern we user is called Singleton. The pattern ensures that the class is instantiated only once and that all requests are directed to that one and only object.

Singleton Pattern-Creational Patterns

The Singleton pattern is a specialist creational pattern as it's primary focus is to facilitate a single shared instance of our object rather than to decouple our client from the object's implementation as with the other creational patterns.

Singleton Pattern and Abstract Class


I know what Singleton Pattern means and Abstract class means.

What I wanted to know was how would this apply to real world.

Could anyone give me any good example or simple explanation.

Say I have a simple website, why would I use any of the above if any.

Why would it simplify my architechture.

Thanks in advace. 

C++ Q&A: Locking Column Headers, Implementing Singleton Classes


Prevent the sizing of the column headers in an ATL composite control. Also, see how you can share a small amount of simple data among multiple processes running on the same machine without Remoting.

Paul DiLascia

MSDN Magazine June 2003

Singleton Pattern


earlier today i was readind about singleton pattern

now i m trying to implement in a testing project

which is like -

public class prodcuttable
    private static prodcuttable _instance = new prodcuttable();
    public static prodcuttable Instance()
        return _instance;

    private prodcuttable()

    public DataTable loadtable()
        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn();
        dc.ColumnName = "ID";
        dc.DataType = Type.GetType("System.Int32");

        dc.ColumnName = "Name";
        dc.DataType = Type.GetType("System.String");
        DataRow dr = dt.NewRow();
        dr["ID"] = 1;
        dr["Name"] = "Aftab";

        return dt;
        //throw new NotImplementedException();

but when i m trying to implement in my Default page

binding with gridview

if (!IsPostBack)
            GridView1.DataSource = prodcuttable.Instance();

its giving me error

The type initializer for 'prodcuttable

"Singleton" pattern in WF 4.0


I am trying to do a singleton pattern with WF and AppFabric. I  want just one instance of a workflow running in the cluster.

I have tried a biztalk style: a method (CallService()) that creates the instance, and the same method in other receive activity (CallService()) that do not have the cancreateinstance checked. (I think I could correlate it through the xpath to the action in the soap message, but lets forget about correlating at this step).

My problem is that WF always creates the instance and does not correlate in the second call.

Do you know how to solve it? I would like to priorize the correlation against the creation of the instances. Any other way to do it?

Thanks in advance.

Singleton Pattern -How to implement in Application_Start() in Global.asax


need to know how we can implement the singleton Pattern in Global.asax file in Application_Start()

Suppose This is my E.g

 public static string Name { get; set; }
    public static string ID { get; set; }

    //singleton pattern implemented ....gud
    private static Employee _emp = new Employee();

    public static Employee EmpInstance()
        return _emp;

    public void Add()
        string sql = "insert into emp_master values('" + ID + "','" + Name + "')";


on my Button Click


        Employee e1 = Employee.EmpInstance();

        Employee.ID = "2";
        Employee.Name = "asdasd";


now somewhere i heard it's best to decalre in Application_Start()

but how i can declare...any hint.





Dialogs and ViewModel - Using Tasks as a Pattern

The ViewModel/MVVM pattern continues to gain popularity, with a blog post showing up every so often, and with tweets and retweets popping up even more often :-). At the same time, there are some interesting topics beyond the core pattern that continue to fuel experimentation. A big one amongst those is how should applications use dialogs when using the view model pattern.

The crux of the problem is the desire to keep the view model independent of UI concerns, and ensure it can be tested in a standalone manner, but that often comes to odds when you want the view model to launch a dialog, and/or do some work after the dialog is closed.

Prototype Design Pattern in C#. Vb.NET

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype

Adapter Design Pattern in C#, VB.NET

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

The Decorator Pattern & Extension Methods

The decorator pattern provides a formal way to add new functionality to an existing type, without sub-classing. First question: What is wrong with classical inheritance? In and of itself, nothing is wrong with the good old Is-A relationship. It is very common to derive a new class from an existing base class in order to override a few virtual members, and add a pinch of new functionality.

LINQ : Implementing IN and NOT IN

I got tried of typing

var result = from s in source
where items.Contains(s)
select s;and so I implemented the IN and NOT IN methods as extension methods:

Factory Pattern in asp.net

The job of the Factory design pattern is to create concrete sub classes. You can see the Factory design pattern used throughout the .NET Framework.

The essence of the Factory Pattern is to "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses." Factory methods encapsulate the creation of objects. This can be useful if the creation process is very complex, for example if it depends on settings in configuration files or on user input.

The Factory Design Pattern

Software architects generally think in terms of high-level abstractions rather than low-level programming details. Representing a system in terms of high-level abstractions promotes understanding of the system and reduces its perceived complexity. One such set of abstractions is software design patterns. They have been successfully applied in the past to simplify and solve recurring problems in software design.

Abstract Factory Design Pattern (Sample in C# and VB .NET)

An abstract factory provides an interface for creating families of related objects without specifying their concrete classes. Sometimes one wants to construct an instance of one of a suite of classes, deciding between the classes at the time of instantiation. In order to avoid duplicating the decision making everywhere an instance is created, we need a mechanism for creating instances of related classes without necessarily knowing which will be instantiated.
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend