using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Instanse
{
class Program
{
static void Main(string[] args)
{
CountInstance instance1 = CountInstance.getInstance();
int iCount = instance1.getCount();
Console.WriteLine("instance1.iCount = {0}", iCount);
CountInstance instance2 = CountInstance.getInstance(); /*单例模式下 获取的instance应该是一致的*/
iCount = instance2.getCount();
Console.WriteLine("instance2.iCount = {0}", iCount);
}
}
class CountInstance
{
private static CountInstance instance = null;
private int iCount = 0;
public static CountInstance getInstance()
{
if (instance == null)
instance = new CountInstance();
return instance;
}
private CountInstance() { }
public int getCount()
{
this.iCount++;
return this.iCount;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Instanse
{
class Program
{
static void Main(string[] args)
{
CountInstance instance1 = CountInstance.getInstance();
int iCount = instance1.getCount();
Console.WriteLine("instance1.iCount = {0}", iCount);
CountInstance instance2 = CountInstance.getInstance(); /*单例模式下 获取的instance应该是一致的*/
iCount = instance2.getCount();
Console.WriteLine("instance2.iCount = {0}", iCount);
}
}
class CountInstance
{
private static CountInstance instance = null;
private int iCount = 0;
public static CountInstance getInstance()
{
if (instance == null)
instance = new CountInstance();
return instance;
}
private CountInstance() { }
public int getCount()
{
this.iCount++;
return this.iCount;
}
}
}