C# 实例构造函数与静态构造函数

发布于 2024-09-25 18:47:52 字数 50 浏览 6 评论 0 原文

两者有何区别?我只使用了一种构造函数,我相信它是静态构造函数。只熟悉C++和Java。

What are the differences between the two? I've only used one kind of constructor and I believe it's the static constructor. Only familiar with C++ and Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此刻的回忆 2024-10-02 18:47:52

第一次引用类时调用静态构造函数,即

MyClass.SomeStaticMethod()

每次执行“MyClass dummy = new MyClass()”时调用实例构造函数,即创建类的实例

语义上首先在您想要时使用确保一些静态状态在访问之前被初始化,另一些用于初始化实例成员。

Static constructor is called the first time your class is referenced i.e.

MyClass.SomeStaticMethod()

Instance constructor is called every time you do 'MyClass dummy = new MyClass()' i.e. create instance of the class

Semantically first is used when you want to ensure that some static state is initialized before it is accessed, the other is used to initialize instance members.

多情出卖 2024-10-02 18:47:52

静态构造函数允许您初始化类中的静态变量,或者在代码中首次引用类后执行类中需要执行的其他操作。每次程序运行时它们仅被调用一次。

静态构造函数是使用此语法声明的,并且不能重载或具有任何参数,因为它们在您的类通过其名称引用时运行:

static MyClass()
{
}

实例构造函数是在您创建新对象(类的实例)时调用的构造函数。它们也是您通常在 Java 和大多数其他面向对象语言中使用的语言。

您可以使用它们来为新对象提供初始状态。这些可以重载,并且可以带参数:

public MyClass(int someNumber) : this(someNumber, 0) {}

public MyClass(int someNumber, int someOtherNumber)
{
    this.someNumber = someNumber;
    this.someOtherNumber = someOtherNumber;
}

调用代码:

MyClass myObject = new MyClass(100, 5);

Static constructors allow you to initialize static variables in a class, or do other things needed to do in a class after it's first referenced in your code. They are called only once each time your program runs.

Static constructors are declared with this syntax, and can't be overloaded or have any parameters because they run when your class is referenced by its name:

static MyClass()
{
}

Instance constructors are the ones that are called whenever you create new objects (instances of classes). They're also the ones you normally use in Java and most other object-oriented languages.

You use these to give your new objects their initial state. These can be overloaded, and can take parameters:

public MyClass(int someNumber) : this(someNumber, 0) {}

public MyClass(int someNumber, int someOtherNumber)
{
    this.someNumber = someNumber;
    this.someOtherNumber = someOtherNumber;
}

Calling code:

MyClass myObject = new MyClass(100, 5);
想挽留 2024-10-02 18:47:52

静态构造函数对于类的所有实例或使用仅运行一次。它将在您第一次使用该类时运行。当您实例化类的对象时,普通构造函数就会运行。

您需要了解的有关静态构造函数的所有信息都可以在这里找到:https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors

The static constructor runs only once for all instances or uses of the class. It will run the first time you use the class. Normal constructors run when you instantiate an object of the class.

Everything you should need to know about static constructors can be found here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文