对于结构体,我是否必须在 C# 中显式调用构造函数?

发布于 2024-08-31 11:48:12 字数 89 浏览 8 评论 0原文

问题是关于结构的。当我声明一个结构类型变量/对象(不知道哪一个更适合)或一个数组或结构列表时,我是否必须像对象一样显式调用构造函数,或者只是像变量一样声明就足够了?

The question is about the structs. When I declare a struct type variable/object (don't know which one suits better) or an array or list of structs, do I have to call the constructor explicitly like objects or just declaring will suffice like variables?

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

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

发布评论

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

评论(2

柠檬 2024-09-07 11:48:12

C# 中的结构体可以通过调用或不调用构造函数来创建。在没有调用构造函数的情况下,struct 将被初始化为默认值(基本上归零),并且无法使用 struct直到它的所有字段都被初始化。

从文档中:

当您使用以下命令创建结构体对象时
新的运算符,它被创建并且
调用适当的构造函数。
与类不同,结构可以是
实例化时不使用 new
操作员。如果不使用 new,则
字段将保持未分配状态,并且
直到所有的对象都不能被使用
字段已初始化。

下面是一些示例:

struct Bar
{ 
   public int Val;  

   public Bar( int v ) { Val = v; }
}

public void Foo()
{
    Bar z;      // this is legal...
    z.Val = 5;

    Bar q = new Bar(5); // so is this...
    q.Val = 10;

    // using object initialization syntax...
    Bar w = new Bar { Val = 42; }
}

结构数组与单个结构变量不同。当您声明结构类型的数组时,您正在声明一个引用变量 - 因此,您必须使用 new 运算符分配它:

Bar[] myBars = new Bar[10];  // member structs are initialized to defaults

如果您的结构具有构造函数,您还可以选择使用数组初始化语法:

Bar[] moreBars = new Bar[] { new Bar(1), new Bar(2) };

你可以变得比这更复杂。如果您的 struct 具有原始类型的隐式转换运算符,您可以像这样初始化它:

struct Bar
{ 
   public int Val;  

   public Bar( int v ) { Val = v; }

   public static implicit operator Bar( int v )
   {
       return new Bar( v );
   }
}

// array of structs initialized using user-defined implicit converions...
Bar[] evenMoreBars = new Bar[] { 1, 2, 3, 4, 5 };

Structs in C# can be created with or without invoking a constructor. In the case when no constructor is invoked, the members of the struct will be initialized to default values (essentially zeroed out), and the struct cannot be used until all of its fields are initialized.

From the documentation:

When you create a struct object using
the new operator, it gets created and
the appropriate constructor is called.
Unlike classes, structs can be
instantiated without using the new
operator. If you do not use new, the
fields will remain unassigned and the
object cannot be used until all of the
fields are initialized.

Below are some examples:

struct Bar
{ 
   public int Val;  

   public Bar( int v ) { Val = v; }
}

public void Foo()
{
    Bar z;      // this is legal...
    z.Val = 5;

    Bar q = new Bar(5); // so is this...
    q.Val = 10;

    // using object initialization syntax...
    Bar w = new Bar { Val = 42; }
}

Arrays of structs are different than a single struct variable. When you declare an array of a struct type you are declaring a reference variable - as such, you must allocate it using the new operator:

Bar[] myBars = new Bar[10];  // member structs are initialized to defaults

You can also choose to use array initialization syntax if your struct has a constructor:

Bar[] moreBars = new Bar[] { new Bar(1), new Bar(2) };

You can get more sophisticated than this. If your struct has an implicit conversion operator from a primitive type, you can initialize it like so:

struct Bar
{ 
   public int Val;  

   public Bar( int v ) { Val = v; }

   public static implicit operator Bar( int v )
   {
       return new Bar( v );
   }
}

// array of structs initialized using user-defined implicit converions...
Bar[] evenMoreBars = new Bar[] { 1, 2, 3, 4, 5 };
挥剑断情 2024-09-07 11:48:12

Struct 是 C# 中的一种值类型,因此它使用堆栈内存而不是堆。

您可以按照常规方式声明结构体变量,例如int a = 90;

int是C#中的结构体类型。

如果您使用 new 运算符,则会调用相应的构造函数。

Struct is a Value Type in C#, so it uses Stack memory rather than Heap.

You can declare a struct variable in the regular way e.g int a = 90;,

int is a struct type in c#.

If you use new operator then the corresponding constructor will be invoked.

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