C#编译器错误:“结构中不能包含实例字段初始化程序” [英] C# compiler error: "cannot have instance field initializers in structs"

查看:93
本文介绍了C#编译器错误:“结构中不能包含实例字段初始化程序”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关结构的建议。

I need advice on structures.

我有2部分代码。第一部分如下:

I have 2 sections of code. The first section is as below:

namespace Project.GlobalVariables
{
    class IOCard
    {
        struct InputCard
        {
            public string CardNo;
            public int BaseAddress;
            public int LowerAddress;
            public int UpperAddress;
            public int[] WriteBitNo = new int[16];
            public int[] ReadBitNo = new int[16];
        }

        static InputCard[] InputCards = new InputCard[5];

        public static string ACardNo = InputCards[1].CardNo;
        public static string BCardNo = InputCards[2].CardNo;

    }
}

第二部分如下:

private void Form1_Load(object sender, EventArgs e)
    {
        IOCard.ACardNo = "Card A";
        IOCard.BCardNo = "Card B";

        MessageBox.Show(IOCard.ACardNo);
        MessageBox.Show(IOCard.BCardNo);
    }

我的计划是能够分配和检索通过使用 IOCard 来输入InputCards 组件,如 Form1_Load 中所示。

My plan is to be able to assign and retrieve InputCards component by using IOCard as shown in Form1_Load.

但是,当我编译代码时,出现以下错误。

However, when I compile the code, I get the following error.


错误1'Project.GlobalVariables .IOCard.InputCard.WriteBitNo':不能在结构E中使用实例字段初始化程序:E:\编程\新平台\StandardPlatform\StandardPlatform\Project\GlobalVariables.cs 16 26 StandardPlatform

Error 1 'Project.GlobalVariables.IOCard.InputCard.WriteBitNo': cannot have instance field initializers in structs E:\Programming\New platform\StandardPlatform\StandardPlatform\Project\GlobalVariables.cs 16 26 StandardPlatform

有人可以告诉我如何解决错误吗?
请告知。谢谢。

Can someone tell me how to solve the error? Please advise. Thanks.

以下是我尝试创建和使用的类,但失败了。

Here are the classes that I have attempted to create and use, but failed.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project.GlobalVariables
{
    static class IOCard
    {
        public const int TotalInputCard = 10;
        public const int TotalOutputCard = 10;

        public  class InputCard
        {
            public string CardNo = "1";
            public int BaseAddress;
            public int LowerAddress;
            public int UpperAddress;
            public int[] WriteBitNo = new int[16];
            public int[] ReadBitNo = new int[16];
        }

        public class OutputCard
        {
            public string CardNo;
            public int BaseAddress;
            public int LowerAddress;
            public int UpperAddress;
            public int[] WriteBitNo = new int[16];
            public int[] ReadBitNo = new int[16];
        }

        public static InputCard[] InputCards = new InputCard[TotalInputCard];
        public static OutputCard[] OutputCards = new OutputCard[TotalOutputCard];

        public static int X100 = InputCards[0].WriteBitNo[0];
        public static int Y100 = OutputCards[0].WriteBitNo[0];
    }
}

我尝试在 Form_Load ,如下所示:

private void Form1_Load(object sender, EventArgs e)
{
    IOCard.X100 = 1;
    IOCard.Y100 = 1;
} 

无论我试图在网上搜索多少答案,我

No matter how much I have tried to search on the net for answers, I have got nowhere.

请告知。谢谢。

推荐答案

在C#中, struct 的值是不是以 class 类型的值的方式引用 struct 的值是 struct 的实例字段的所有值的联合。

In C#, a struct value is not a reference to an object in the way a value of a class type is. The value of a struct is the "union" of all the values of the instance fields of the struct.

现在,结构类型的默认值是所有这些字段都有其 默认值。自C#开始以来,语法为:

Now, the default value of a struct type is the value where all those fields have their default values. Since the beginning of C#, the syntax:

new S()  // S is a value-type

其中 S 是结构类型,等效于默认值该结构的值。没有构造函数调用!这是可以(当今)也可以写入的完全相同的值

where S is a struct type, has been equivalent to the default value of that struct. There is no constructor call! This is the exact same value which can (nowadays) also be written

default(S)  // S is a value-type

现在,类似

struct S
{
  int field = 42; // non-static field with initializer, disallowed!

  // ...
}

是非法的( 在结构中不能有实例字段初始化程序)。他们给人的印象是 new S()字段将是 42 ,但实际上 new S()字段必须为默认值 int (该数字为零,与 42 不同)。

are illegal (cannot have instance field initializers in structs). They could give the impression that the field of a new S() would be 42, but in fact the field of new S() must be the default value of int (which is zero, distinct from 42).

使用此说明,您还将看到为什么无法为结构类型创建非静态的零参数构造器的原因,使用C#。

With this explanation, you also see why it is not possible to create a non-static, zero-parameter constructor for a struct type, in C#.

这篇关于C#编译器错误:“结构中不能包含实例字段初始化程序”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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