如何设置自动属性支持字段的结构构造函数的价值? [英] How can I set the value of auto property backing fields in a struct constructor?

查看:190
本文介绍了如何设置自动属性支持字段的结构构造函数的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于这样的结构:

public struct SomeStruct
{
    public SomeStruct(String stringProperty, Int32 intProperty)
    {
        this.StringProperty = stringProperty;
        this.IntProperty = intProperty;
    }

    public String StringProperty { get; set; }
    public Int32 IntProperty { get; set; }
}



当然,将产生一个编译器错误读取在'之前,其所有字段都被分配到

这'对象不能用是否有值分配给支持字段或属性本身的一种方式,还是我要实现性能与我自己的明确的支持领域老式的方法?

Is there a way to assign values to the backing fields or the properties themselves, or do I have to implement properties the old-fashioned way with my own explicit backing fields?

推荐答案

您需要使用这个构造:

public SomeStruct(String stringProperty, Int32 intProperty) : this()
{
    this.StringProperty = stringProperty;
    this.IntProperty = intProperty;
}



这样做调用默认的构造函数,并通过这样做,它初始化所有字段,从而使这个在自定义构造函数引用。

Doing this calls the default constructor and by doing so, it initializes all the fields, thus allowing this to be referenced in the custom constructor.

这篇关于如何设置自动属性支持字段的结构构造函数的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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