编译器为结构时,未初始化错误,如果我们试图访问该财产,但不具有可变 [英] Compiler gives error when struct is not initialized and if we try to access the property but not with variable

查看:179
本文介绍了编译器为结构时,未初始化错误,如果我们试图访问该财产,但不具有可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

然后,它变得清晰,以搜索内容的在说明书或在<一个href="http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=C%23+struct+definitely+assigned">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.

这篇关于编译器为结构时,未初始化错误,如果我们试图访问该财产,但不具有可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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