.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


Posted By:      Posted Date: September 29, 2010    Points: 0   Category :ASP.Net

Hi there,

I have a question about using a keyword static in front of a function.

I have a Utility class that are used to by other class functions. The functions in the Utility class is just calculation based on the parameter. There could be multiple objects calling these functions at the same time. Can I still use "static" keyword?

Would this make the program run even a little bit faster? Am I mistaking the whole point of using "static"?


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.

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. 



I have a class that is created as a singleton because its used to access the filesystem in a multi-threaded app.

As part of the class instantiation I need to assign some values to an internal list used by the class when I set an instance of it.

Heres some bare bones code for what im trying to do

The singleton class
sealed public class Validator : IValidator

	set up some private variable here...
	List<blah> lst = new List<blah>();  basically 1 or more  pairs of strings

	public static Validator Instance

		code here to add items to the List (lst)?
		SchemaSet = new XmlSchemaSet();
		for each (blah b in lst)
			SchemaSet.Add(b.value1, b.value2);	
		more code here.......

and the code to use an instance of my class

private Validator SValidator;
SValidator = Validator.Instance;  -- add items to the List here?

so when I create an instance of Validator, how can I assign values to the list when its being created, something like

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 class and multithreading


I have a singleton class thats used throughout an application.  The app will be multi threaded, each thread will access methods in the singleton class.  Is it best to create an instance of the singleton class at application startup then pass that into each thread, or is it safe to create an instance in each thread separately ?  (I check that no instance currently exists in the singleton class itself, so only 1 instance will ever be invoked)

Singleton and Interface

I want to implement the interface singleton, same like generic singleton.   public class Singleton<T>  { } Singleton<OnlineUser>..   So how to implement Singleton<IOnlineUser> ?    

UserControl Resouces Singleton Instance Question


Given, a viewmodel referred to in a UserControl reource like this:

 <local:MainVM x:Key="VM"></local:MainVM>

Everything works great with respect to finding resources etc.

Now the usercontrol has bindings like this:

      DataContext="{Binding Sour

Slow performance of WCF service calling singleton (which calls COM+!)


WCF Service call routing to COM+ application.
netTCPBinding, throttling set pretty high.

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Multiple)] 

Service has two methods, one simply returns the string was input, the other calls the COM+ component and FOR TESTING returns the string "success" for every call. Therefore, both methods return a simple string.

Client starts 20 threads, each calling the WCF Service twice. (NOTE: A "first call" is made to prime the singleton service and also the COM+ component)

Using log4net and a Stopwatch to log the timings of the clien

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.

How do I register C# COM server as a singleton?


I have a C# .EXE that implements a COM interface that needs to be a singleton.  However, I have been unable to register the .EXE so that only a single instance will be shared between multiple processes.  Currently the .exe appears to be loaded into each process space as if it were a .DLL.

Each instance works correctly except that the resources owned by the COM server are not shared between processes.

I'm reasonable sure that the only problem is how the .EXE is registered.

Native C++ included in C# - problem with native singleton


Hi all

i am trying to integrate a static native c++ lib into a c# project. I use a c++/cli intermediate layer
which wraps the native classes and exposes them to the managed layer. That has worked fine so
far, i even managed to overcome the problem of using Boost library in the native classes.
I use VS 2005.

I now have a problem when i try to instantiate a singleton within the native code, which is implemented
according to the following principle

public : static Singleton* exemplar();
: Singleton() {}
: static Singleton * instanz;

This singleton class is not wrapped in the c++/cli layer but it is constructed within an exposed native class.

The code compiles and links fine, but when i start the c#-exe i receive the error message:
ModuleLoadException: The C++ module failed to load during native initialization
ie the program doesn't even reach any executable sections.

Is that a known problem?
Can anybody point me towards a solution?



Singleton, Depedency Injection, HttpContext.Current.Cache or something else?


I'm currently writing a webapp in MVC where I need to have a Request ID number for each submission that users send in. I originally wrote the class using a Singleton pattern which would contain "private static List<Request>" and then public accessor methods allow the list to be manipulated, updated, etc. -- So when hitting a new request page, the user would be assigned a new unique request ID and this would ID would be stored in the List<Request> of the Singleton with status, etc. and stay with them until they close the window or complete the request. So it would be a unique request number, but also give me a unique way of controlling the session and preventing double submissions or re-submissions of the same request.

I thought that this was brilliant until it was explained to me on StackOverflow that each page request is started in its own process and runs to completion and then the process dies, and the singleton with it. So with each new page request starts a new process with no knowledge of the previous singleton that was in memory.

The smart solution would be to store this in a DB, but unfortunately, I have none immediately available to me at the moment, so I'm storing the very last Request ID number in web.config. It's a hack, I realize...

So because of the way MVC works, it seems that Singleton and Static Clas

When is Dispose called for a ReleaseInstanceName.None Singleton WCF Web service?



When is Dispose called for a Singleton WCF Web service with a default release instance? (That is, ReleaseInstanceName.None)

Has WCF changed recently with .NET 4.0?

I'm running a singleton WCF Web service with embedded "experimental" Python libs and cannot debug the service.



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