编译器为结构时,未初始化错误,如果我们试图访问该财产,但不具有可变编译器、但不、初始化、财产

由网友(風の沙)分享简介:我有一个观察约结构。当我在结构体声明一个属性,如果我不初始化结构体,然后它给了我下面的错误 - 使用未分配的局部变量empStruct的I have one observation about struct. When I declare a property in Struct and if I don't ini...

我有一个观察约结构。当我在结构体声明一个属性,如果我不初始化结构体,然后它给了我下面的错误 - 使用未分配的局部变量empStruct的

I have one observation about struct. When I declare a property in Struct and if I don't initialize the Struct then it gives me the below error - "Use of unassigned local variable empStruct"

PSeduo code -

PSeduo Code-

struct EmpStruct
{
    private int firstNumber;
    public int FirstNumber
    {
        get { return firstNumber; }
        set { firstNumber = value; }
    }

    public int SecondNumber; 

}

Program.cs中 -

Program.cs-

EmpStruct empStruct;
empStruct.FirstNumber = 5;

但是,当我宣布公共变量则高于code ++工程。

But when I declare public variable then the above code works.

EmpStruct empStruct;
empStruct.SecondNumber;

所以我的问题是,为什么编译器没有给出错误,当我尝试访问变量(如遇类,它会给错误)。

So my question is why compiler not gives error when i try to access variable.(In case of Class it will give the error).

推荐答案

有一个在这个线程混淆的大量。

There's a tremendous amount of confusion in this thread.

其原理是这样的:直到所有的结构肯定是分配的,你不能调用的实例中的任何属性或方法的一个实例的领域

The principle is this: until all of the fields of an instance of a struct are definitely assigned, you can not invoke any properties or methods on the instance.

这就是为什么code你的第一个程序段将无法编译。您正在访问属性没有明确指定的所有字段。

This is why your first block of code will not compile. You are accessing a property without definitely assigning all of the fields.

的code中的第二块编译,因为它的好来访问一个字段,不都被明确赋值的字段。

The second block of code compiles because it's okay to access a field without all of the fields being definitely assigned.

要明确指定结构是说一个方法

One way to definitely assign a struct is to say

EmpStruct empStruct = new EmpStruct();

这将调用默认的无参数的构造函数用于 EmpStruct 这肯定会分配所有的字段。

This invokes the default parameterless constructor for EmpStruct which will definitely assign all of the fields.

该规范的有关条文是第5.3节上明确赋值。而从第11.3.8节的例子

The relevant section of the specification is §5.3 on Definite Assignment. And from the example in §11.3.8

没有实例成员函数(包括set访问属性 X )的调用,直到所有该结构的领域正在建设中已明确赋值。

No instance member function (including the set accessors for the properties X and Y) can be called until all fields of the struct being constructed have been definitely assigned.

这将是更有帮助的(啊哈,埃里克利珀!)如果编译器错误信息是沿着线

It would be more helpful (ahem, Eric Lippert!) if the compiler error message were along the lines of

使用不是明确赋值的局部变量 empStruct

Use of not definitely assigned local variable empStruct.

然后,它变得清晰,以搜索内容的在说明书或在Google.

Then it becomes clear what to search for the in the specification or on Google.

现在,请注意,已定义的一个可变的结构。这是危险的,邪恶的。你不应该这样做。相反,添加一个公共构造函数,可以让你绝对分配 firstNumber secondNumber ,并从公共二传手 EmpStruct.FirstNumber

Now, note that you've defined a mutable struct. This is dangerous, and evil. You shouldn't do it. Instead, add a public constructor that lets you definitely assign firstNumber and secondNumber, and remove the public setter from EmpStruct.FirstNumber.

阅读全文

相关推荐

最新文章