当前位置: 首页 > 图文教程 > .Net技术 > C# > My Singleton in C#

C#
C#和Java的区别
提高C#编程水平的50个要诀
GridView 删除/更新/取消
c#线程
C#泛型有什么好处
总体了解C#
C#2.0匿名函数
GridView中添加一个CheckBox列
C#2.0介绍之Iterators(迭代器)
.NET与Java间进行Web Service交互的选择
C# 2010命名和可选参数的新特性
利用C#远程存取Access数据库
C#中foreach基础使用方法
C#中用鼠标移动页面功能的实现
C# 4.0中泛型协变性和逆变性详解
C#:C# .Net中的类型相互转换教程
C#:C#中的基元类型
C#:语言中的重要知识详细介绍与解释
C#:浅谈C#中的集合对象(Collections)
C#:C#发起邮件会议

My Singleton in C#


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 65 ::
收藏到网摘: n/a

//MySingleton
using System;
//SingletonPage Class
class SingletonPage
{
 //Fields
 protected static SingletonPage checkoutpage;
 

 //Constructor is protected to ensure Singleton
 protected SingletonPage()
 {
  Console.WriteLine("if you see this line,then the only one instence is created!");
 }

 //Use this to Create SingletonPage instance
 public static SingletonPage NewCheckOutPage()
 {
  if (checkoutpage==null)
     checkoutpage= new SingletonPage();
  return checkoutpage;
 }

};
//-------------------------------------End of SingletonPage Class


//TestApp
class TestApp
{
 public static void Main(string[] args)
 {
  Console.WriteLine("'create' pagea:");
  SingletonPage pagea=SingletonPage.NewCheckOutPage();

        Console.WriteLine("'create' pageb:");
  SingletonPage pageb=SingletonPage.NewCheckOutPage();
  
  Console.WriteLine("'create' pagec:");
  SingletonPage pagec=SingletonPage.NewCheckOutPage();
  
  Console.WriteLine("'create' paged:");
  SingletonPage paged=SingletonPage.NewCheckOutPage();

  while(true){}
 }
};