当 struct 未初始化并且我们尝试访问属性而不是变量时,编译器会给出错误 [英] Compiler gives error when struct is not initialized and if we try to access the property but not with variable

查看:19
本文介绍了当 struct 未初始化并且我们尝试访问属性而不是变量时,编译器会给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 struct 的观察.当我在 Struct 中声明一个属性并且如果我没有初始化 Struct 时,它会给我以下错误 - 使用未分配的局部变量 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 代码-

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;

但是当我声明公共变量时,上面的代码就可以工作了.

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.

原理是这样的:在struct的实例的所有字段都被明确赋值之前,你不能调用实例上的任何属性或方法.

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.

这就是您的第一个代码块无法编译的原因.您正在访问一个属性,而没有明确分配所有字段.

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

第二个代码块可以编译,因为可以在没有明确分配所有字段的情况下访问一个字段.

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

明确分配struct的一种方法是说

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

任何实例成员函数(包括属性XY 的集合访问器)都不能被调用,直到正在构造的结构的所有字段都被明确分配.

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.

现在,请注意您已经定义了一个可变结构.这是危险的,也是邪恶的.你不应该这样做.相反,添加一个公共构造函数,让您明确分配 firstNumbersecondNumber,并从 EmpStruct.FirstNumber 中删除公共 setter.

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.

这篇关于当 struct 未初始化并且我们尝试访问属性而不是变量时,编译器会给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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