c# - 使用 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()
?
谢谢
解决方案
推荐阅读
- android - 为什么查询参数不使用 Retrofit2 android 编码
- hyperledger-composer - 如何在本地使用超级账本网卡
- php - 从 gmail 复制客户端数据并提交表单时 PHP 抛出 403 禁止错误
- php - 在 Drupal8 自定义块中使用自定义树枝模板
- c++ - Android 上 __property 的 Embarcadero C++Builder 错误
- ios - Xcode 10 - 构建和安装后未更新应用程序
- python-3.x - 带有迁移学习的自动编码器?
- java - 如何使用声明范围之外的对象?
- php - 填补时间表中的空白
- java - 在非活动类中保存值