static vs Instance构造函数 [英] static vs Instance constructors

查看:69
本文介绍了static vs Instance构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我试用了一段代码:





  class  gen< t> 
{
静态 int count = 0 ;
static gen()
{
Console.WriteLine( 已创建:{0},count.ToString());
count ++;
}

}





 < span class =code-keyword> class 计划
{
静态 void Main( string [] args)
{

gen< string> g1 = new gen< string>();
gen< int> g2 = new gen< int>();
gen< guid> g3 = new gen< guid>();



gen< string> g11 = new gen< string>();
gen< int> g22 = new gen< int>();
gen< guid> g33 = new gen< guid>();




Console.ReadLine();

}
}







- 它打印:

创建:0< br /> 
创建:0< br />
创建:0



- 它为每种类型打印一次。如果我使用实例构造函数,那么输出将是:

 created:0< br /> 
created:0< br />
created: 0< br />
创建:1< br />
创建:1< br />
创建:1< br />



- 这意味着每次都会初始化实例构造函数。现在我的疑问是,如果使用静态构造函数,那么对象只在堆上创建一次而不是实例构造函数?

- 接下来是:最好使用什么:static或实例构造函数。

- 因为通过使用实例构造函数,我可以根据场景多次传递参数:这意味着我可以生成各种结果集。我是否正确?

- 何时使用实例构造函数以及何时使用静态构造函数?



谢谢和问候,

-Rahul

解决方案

作为第一次调用类的静态构造函数被引用

 TestClass.MyStaticMethod()



因此,当您想要初始化任何静态数据或执行仅需要执行一次的特定操作时,您需要创建静态构造函数。



只要您想创建和初始化使用新表达式创建类的对象的任何实例成员变量,就使用Instance构造函数。


1.通常,静态构造函数用于初始化类的静态成员,并且在创建该类型的新对象之前或者在调用其任何静态成员之前调用它。因此静态构造函数不能手动调用,并且在访问类的任何成员和方法之前由.NET自动调用它们(包括非静态构造函数)!!!



2.在你的例子中,你使用模板并且更复杂,但原理是相同的:当你为给定的模板类型调用实例构造函数时,那么只有在第一次模板类型时使用,在你执行构造函数代码之前,静态构造函数的执行就像上面的第一条规则所说的那样。



PS:为了更好地理解静态构造函数我建议您首先测试它们的正常类(不使用模板),然后通过使用模板使问题复杂化。


MSDN写道:

静态构造函数用于初始化任何静态数据,或执行需要的特定操作仅执行一次 在创建第一个实例或引用任何静态成员之前自动调用它。





我建议从基础开始:

使用构造函数(C#编程指南) [ ^ ]

静态构造函数(C#编程指南) [ ^ ]

实例构造函数(C#编程指南) [ ^ ]

类(C#编程G. uide) [ ^ ]

静态类和静态类成员(C#编程指南) [ ^ ]

抽象和密封的类和类成员(C#编程指南) [ ^ ]

访问修饰符(C#编程指南) [ ^ ]

对象和集合初始化器(C#编程指南) [ ^ ]

反射和通用类型 [ ^ ]

Auto-Implemented Properties(C#编程指南) [ ^ ]



您可能正在寻找类似的东西: C#类自动增量ID [ ^ ]


Hello,

I experimented with a code:


class gen<t>
   {
       static int count = 0;
       static gen()
       {
           Console.WriteLine("created: {0}",count.ToString());
           count++;
       }

   }



class Program
   {
       static void Main(string[] args)
       {

           gen<string> g1 = new gen<string>();
           gen<int> g2 = new gen<int>();
           gen<guid> g3 = new gen<guid>();



           gen<string> g11 = new gen<string>();
           gen<int> g22 = new gen<int>();
           gen<guid> g33 = new gen<guid>();




           Console.ReadLine();

       }
   }




- It prints:

created: 0<br />
  created: 0<br />
  created: 0


- It prints this once for each type. If i use an instance constructor then the output would be:

created: 0<br />
  created: 0<br />
  created: 0<br />
  created: 1<br />
  created: 1<br />
  created: 1<br />


- This means that an instance constructor gets initialized every time. Now my doubt is that if use a static constructor then the object is created just once on the heap as against an instance constructor?
- The next thing is: whats better to use: static or an instance constructor.
- Because by using an instance constructor i can pass arguments multiple times depending upon the scenario: which means that i can produce a variety of result sets. Am i correct?
- When to use an instance constructor and when to use a static constructor?

Thanks and regards,
-Rahul

解决方案

As Static constructor called first time as class is referenced

TestClass.MyStaticMethod()


So you need to create a static constructor when you want to initialize any static data, or to perform a particular action that needs to be performed once only.
and
use Instance constructor whenever you want to to create and initialize any instance member variables that you use the new expression to create an object of a class.


1.In generally a static constructor is used to initialize the static members of a class and it is invoked first time before to create a new object of that type OR before to invoke any of its static members. So the static constructor cannot be invoked manually and they are automatically invoked by the .NET before to access any member and methods of the class (including the non static constructors)!!!

2.In your example you are using templates and is more complicated, but the principle is the same: when you invoke an instance constructor for a given "template type", then ONLY if is the first time when that "template type" is used, before you instance constructor code to be executed, the static constructor is executed like the 1st rule above said.

PS: To understand better the static constructors I recommend you first to test them for normal classes (that are not using templates), then to complicate the problem like in your example by using templates.


MSDN wrote:

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.



I would suggest to start with basics:
Using Constructors (C# Programming Guide)[^]
Static Constructors (C# Programming Guide)[^]
Instance Constructors (C# Programming Guide)[^]
Classes (C# Programming Guide)[^]
Static Classes and Static Class Members (C# Programming Guide)[^]
Abstract and Sealed Classes and Class Members (C# Programming Guide)[^]
Access Modifiers (C# Programming Guide)[^]
Object and Collection Initializers (C# Programming Guide)[^]
Reflection and Generic Types[^]
Auto-Implemented Properties (C# Programming Guide)[^]

You are probably looking for something similar to: C# Class Auto increment ID[^]


这篇关于static vs Instance构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆