简体   繁体   English

公共构造函数和静态构造函数

[英]Public constructor and static constructor

I am reading a code in C# that uses two constructors.我正在阅读使用两个构造函数的 C# 代码。 One is static and the other is public.一个是静态的,另一个是公开的。 What is the difference between these two constructors?这两个构造函数有什么区别? And for what we have to use static constructors?我们必须使用静态构造函数来做什么?

static and public are orthogonal concepts (ie they don't have anything to do with each other). staticpublic是正交的概念(即它们彼此没有任何关系)。

public simply means that users of the class can call that constructor (as opposed to, say, private ). public只是意味着该类的用户可以调用该构造函数(而不是例如private )。

static means that the method (in this case the constructor) belongs not to an instance of a class but to the “class itself”. static意味着该方法(在这种情况下是构造函数)不属于类的实例,而是属于“类本身”。 In particular, a static constructor is called once , automatically, when the class is used for the first time.特别是,静态构造函数在第一次使用时自动调用一次

Furthermore, a static constructor cannot be made public or private since it cannot be called manually;此外,静态构造函数不能被设为publicprivate因为它不能被手动调用; it's only called by the .NET runtime itself – so marking it as public wouldn't be meaningful.它仅由 .NET 运行时本身调用 - 因此将其标记为public没有意义。

Static constructor runs just once, before your class is instantiated.在类被实例化之前,静态构造函数只运行一次。 It's used if you want something to happen just once.如果您希望某事只发生一次,则使用它。 A nice example would be a Bus class (similar to something they explain in MSDN article ):一个很好的例子是 Bus 类(类似于他们在MSDN 文章中解释的内容):

public class Bus
{
    public static int busNo = 0;

    static Bus()
    {
        Console.WriteLine("Woey, it's a new day! Drivers are starting to work.");
    }

    public Bus()
    {
        busNo++;

        Console.WriteLine("Bus #{0} goes from the depot.", busNo);
    }
}


class Program
{
    static void Main(string[] args)
    {
        Bus busOne = new Bus();
        Bus busTwo = new Bus();
    }

    // Output:
    // Woey, it's a new day! Drivers are starting to work.
    // Bus #1 goes from the depot.
    // Bus #2 goes from the depot.
}

Static Constructor... It is guaranteed to be called "once" througout the life of the application/app Domain.静态构造函数...保证在应用程序/应用程序域的整个生命周期中被称为“一次”。 It can contain statements that you want to be executed only once.它可以包含您只想执行一次的语句。

Public Constructor... Since we can not add access modifiers to a static constructor, a public constructor means you are talking about an instance constructor.公共构造函数... 由于我们不能向静态构造函数添加访问修饰符,因此公共构造函数意味着您正在谈论实例构造函数。 If an instance constructor is public then the outside world can create its instances.如果实例构造函数是公共的,那么外部世界就可以创建它的实例。 Other options are Internal ( can be called from within the library), Private ( from within the class only).其他选项是 Internal(可以从库内调用)、Private(仅从类内调用)。

Static Constructor 静态构造函数

A constructor declared using static modifier is a static constructor. 使用static修饰符声明的构造函数是静态构造函数。 A static constructor is use to initialize static data or to perform a particular action that need to be performed only once in life cycle of class. 静态构造函数用于初始化静态数据或执行需要在类的生命周期中仅执行一次的特定操作。 Static constructor is first block of code to execute in class. 静态构造函数是在类中执行的第一个代码块。 Static constructor executes one and only one time in life cycle of class. 静态构造函数在类的生命周期中执行一次且仅执行一次。 It is called automatically. 它会自动调用。 Static constructor does not take any parameters. 静态构造函数不接受任何参数。 It has no access specifiers. 它没有访问说明符。 It is not called directly. 它不是直接调用的。

Instance or Public Constructor 实例或公共构造函数

Instance constructor is used to initialize instance data. 实例构造函数用于初始化实例数据。 Instance constructor is called every time when object of class is created. 每次创建类的对象时都会调用实例构造函数。 It is called explicitly. 它被明确地称为。 Instance constructor takes parameters. 实例构造函数接受参数。 It has access specifiers. 它有访问说明符。

Static constructor called only the first instance of the class created but the public constructor called every time that instance of the class is created. Static constructor只调用创建的类的第一个实例,但每次创建类的实例时都会调用public constructor

Static Constructor静态构造函数

A constructor declared using a static modifier is a static constructor.使用静态修饰符声明的构造函数是静态构造函数。 A static constructor is used to initialize static data or to perform a particular action that needs to be performed only once in the life cycle of class.静态构造函数用于初始化静态数据或执行在类的生命周期中只需要执行一次的特定操作。 A static constructor is the first block of code to execute in class.静态构造函数是在类中执行的第一个代码块。 Static constructor executes one and only one time in the life cycle of class.静态构造函数在类的生命周期中只执行一次。 It is called automatically.它被自动调用。 The static constructor does not take any parameters.静态构造函数不接受任何参数。 It has no access to specifiers.它无法访问说明符。 It is not called directly.它不是直接调用的。

Instance or Public Constructor实例或公共构造函数

Instance constructor is used to initialize instance data.实例构造函数用于初始化实例数据。 Instance constructor is called every time when the object of the class is created.每次创建类的对象时都会调用实例构造函数。 It is called explicitly.它被显式调用。 Instance constructor takes parameters.实例构造函数接受参数。 It has access specifiers.它具有访问说明符。

My source: Static Constructor Vs Instance Constructor in C#我的来源: C# 中的静态构造函数与实例构造函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM