首页 > 解决方案 > 使用 struct 自动实现的属性

问题描述

据我所知,当我将自动实现的属性与 struct 一起使用时,我应该显式链接无参数构造函数(即,必须在分配自动实现的属性之前调用无参数构造函数)。现在我有以下代码可以正常工作而无需链接: this()

    public struct Foo
    {
        public int Value { get; private set; }

        public Foo(int value) //: this()
        {
            Value = value;
        }
    }

    static void Main(string[] args)
    {
        Foo f = new Foo(33);
        Console.WriteLine(f.Value);
    }

我在这里发现了类似的帖子,其中用户遇到了编译器错误,建议使用它: this()来解决问题。为什么我的代码没有: this()? 我们应该使用还是不使用: this()

谢谢

标签: c#struct

解决方案


推荐阅读