私有构造函数是一种特殊的实例构造函数。它通常用在只包含静态成员的类中。如果类具有一个或多个私有构造函数而没有公共构造函数,则其他类(除嵌套类外)无法创建该类的实例。
声明空构造函数可阻止自动生成默认构造函数。注意,如果您不对构造函数使用访问修饰符,则在默认情况下它仍为私有构造函数。但是,通常显式地使用 private 修饰符来清楚地表明该类不能被实例化。
class="code_img_closed" src="/Upload/Images/2013112915/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('2e8383d1-282a-4dc2-bcb2-9109602c2d4f',event)" src="/Upload/Images/2013112915/2B1B950FA3DF188F.gif" alt="" />1 public class PrivateConClass 2 { 3 private static PrivateConClass pcc; 4 5 private PrivateConClass() 6 { 7 Console.WriteLine("This private constructure function. So you cannot create an instance of this class."); 8 } 9 10 public static PrivateConClass CreatePcc() 11 { 12 pcc = new PrivateConClass(); 13 return pcc; 14 } 15 16 public static void ShowStaticMethod() 17 { 18 Console.WriteLine("This is a static method. Just be called by Class name."); 19 } 20 21 public void ShowMethod() 22 { 23 Console.WriteLine("This is a Nonstatic method. Just be called by private static instance pcc."); 24 } 25 } 26 class Program 27 { 28 static Dictionary<string, string> dic = new Dictionary<string, string>(); 29 30 static void Main(string[] args) 31 { 32 PrivateConClass pcc = PrivateConClass.CreatePcc(); 33 pcc.ShowMethod(); 34 PrivateConClass.ShowStaticMethod(); 35 } 36 }View Code
Reference:
http://msdn.microsoft.com/zh-cn/library/kcfb85a6(v=vs.90).aspx