what is singleton class ? [Resolved]

Posted by Yugandhar under Interview Questions on 8/27/2012 | Points: 10 | Views : 1417 | Status : [Member] | Replies : 5
what is singleton class ?
and why?
in which situation we can use




Responses

Posted by: Ranjeet_8 on: 8/28/2012 [Member] [MVP] Gold | Points: 50

Up
0
Down

Resolved
@Yugandhar
Look at this url
http://www.codeproject.com/Articles/1921/Singleton-Pattern-its-implementation-with-C

.

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gopesh9 on: 8/27/2012 [Member] Starter | Points: 25

Up
0
Down
Singleton Classes are the one that allow only single allocations and instances of data, not more than that.

class Program

{
static void Main()
{
MyClass class = MyClass.Instance;
}
}

public sealed class MyClass
{
static readonly MyClass _instance = new MyClass();
public static MyClass Instance
{
get
{
return _instance;
}
}
MyClass()
{
// Initialize.
}
}


See where we are initializing the data of the class. readonly property is necessary here as we cannot initialize it again.

G. S.
.Net Developer

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Yugandhar on: 8/27/2012 [Member] Starter | Points: 25

Up
0
Down
can u giv e c++ code and explain how it is

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gopesh9 on: 8/27/2012 [Member] Starter | Points: 25

Up
0
Down
Sorry i dont know about c++, so i cant help you with that...

G. S.
.Net Developer

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Yugandhar on: 8/28/2012 [Member] Starter | Points: 25

Up
0
Down
thanks @ranjeet

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response