Singleton Pattern in CSharp
来源:互联网 发布:sql 外键关联查询 编辑:程序博客网 时间:2024/05/17 03:03
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
Level Author
Beginner Kunal Cheda
Singleton assures that there is one and only one instance of the class and provides a global point of access to it.There are number of cases in programming where you need to make sure that there can be one and only one instance of a class e.g Window Manager,Print Spooler etc.
Points to Note in the Example Constructor of Class B is private not allowing other classes to Create the instance. Class B has a private static variable(x) which will have the one and one instance of the Class B, and can be accessed through Static method GetB. If GetB is accessed for the first time x will be null ,and will create the instance and return x, from next requests to GetB method it will simply return x.
Save the file as Singleton.cs, Compile C:/>csc Singleton.cs and Run C:/>Singleton
CODE
Singleton.cs
using System;
class A
{
public static void Main(String [] args)
{
B m = B.GetB();
m.j=100; //make changes to instance variable j
Console.WriteLine(m.j);
B x = B.GetB();
//x.j print's 100 which means that there is one and only one instance
Console.WriteLine(x.j);
}
}
class B
{
private static B x;
public int j= 0;
private B()
{
}
public static B GetB()
{
if(x==null)
{
x=new B();
}
return x;
}
}
<
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
Singleton Pattern in CSharp Level Author
Beginner Kunal Cheda
Singleton assures that there is one and only one instance of the class and provides a global point of access to it.There are number of cases in programming where you need to make sure that there can be one and only one instance of a class e.g Window Manager,Print Spooler etc.
Points to Note in the Example Constructor of Class B is private not allowing other classes to Create the instance. Class B has a private static variable(x) which will have the one and one instance of the Class B, and can be accessed through Static method GetB. If GetB is accessed for the first time x will be null ,and will create the instance and return x, from next requests to GetB method it will simply return x.
Save the file as Singleton.cs, Compile C:/>csc Singleton.cs and Run C:/>Singleton
CODE
Singleton.cs
using System;
class A
{
public static void Main(String [] args)
{
B m = B.GetB();
m.j=100; //make changes to instance variable j
Console.WriteLine(m.j);
B x = B.GetB();
//x.j print's 100 which means that there is one and only one instance
Console.WriteLine(x.j);
}
}
class B
{
private static B x;
public int j= 0;
private B()
{
}
public static B GetB()
{
if(x==null)
{
x=new B();
}
return x;
}
}
<
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- Singleton Pattern in CSharp
- Double checking in Singleton pattern
- SingleTon Pattern in C#.net
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Singleton Pattern in C# 5种实现
- [C#] [C# in Depth] Implementing the Singleton Pattern in C#
- singleton pattern
- Singleton Pattern
- Singleton Pattern
- Singleton Pattern
- DataBinding DropDownList
- 杏林同学录(一)
- 用PHP来写记数器(详细介绍)
- WebGain and HP Collaborate to Bridge Java and .NET
- 基于mysql的bbs设计(五)
- Singleton Pattern in CSharp
- 6。bbsd和cq66服务器端改造
- Finding a Control Inside a Template
- PHP+DBM的同学录程序(5)
- String类使用的例子(3)
- PHP+DBM的同学录程序(3)
- 老外编的程序(四)--HashTable的使用示例
- PHP+DBM的同学录程序(4)
- 老外编的程序(五)--如何使用全球标识符(GUID)