构造函数和对象构造之间的区别是什么?

huangapple go评论138阅读模式
英文:

What is the Difference Between Constructors and Object Construction?

问题

我试图通过阅读Microsoft文档来理解自动实现的属性(Auto-Implemented Properties),当文档在页面底部提到三种修改自动属性的方法时:

  • 仅使用 get;
  • 使用 get; init;,以及
  • 使用 get; private set;

根据文档,第一种实现方式用于使属性在构造函数以外的所有地方都不可变。第二种方式仅在对象构建期间不可变。而第三种方式是当您希望属性对所有使用者都不可变时使用的。

get; private set; 很明确它的实现位置,但我对第一种和第二种情况有疑问。在构造函数和 "对象构建" 之间有什么区别?我曾认为它们可以互换使用。但根据我理解,文档暗示它们是不同的。我是否漏掉了什么?还是我误解了?

我尝试在谷歌上搜索并在Stack Overflow上搜索。我看到了许多讨论类似问题的帖子,涉及 get;get; init; 之间的区别,但那对我来说已经很清楚了。我没有找到与我确切问题的答案。

英文:

I was trying to understand Auto-Implemented Properties by reading the Microsoft Documentation.) when it mentioned three ways to modify Auto Properties near the bottom of the page:

  • Using get; only,
  • Using get; init;, and
  • Using get; private set;.

According to the document, the first implementation is used to make the property immutible everywhere except the constructor. The second is immutible during object construction only. And the third is when you want the property to be immutible to all consumers.

get; private set; is clear where that would be implemeted, but my issue comes with the first and second cases. What is the difference between using constructors and "object construction?" I thought they were interchangable. But here, from what I understand, the document is implying that they are differnet. Is there something I am missing? or am I missunderstanding?

I tried googling it and searched in stack overflow. I saw many posts disscusing similar issues reguarding the difference betrween get; and get; init;, but that was already clear to me. I coundn't find my exact question being answered.

答案1

得分: 3

类C
{
    公共整数 Prop1 { 获取; } = 2;
    公共整数 Prop2 { 获取; 初始设置; } = 4;

    // 构造函数
    公共C()
    {
        // 只能在这里分配Prop1
        Prop1 = 6;
        // 可以在这里分配Prop2,但也可以在“对象初始化器”中分配
        Prop2 = 8;
    }
}

// 大括号内的内容是文档所指的“对象初始化器”
var c = new C()
{
    // 下一行会出错
    // Prop1 = 10,

    // 设置Prop2没有问题,
    // 这也会覆盖构造函数中设置的值
    Prop2 = 10
}

`Prop2` 在这些大括号结束后无法再次设置,这是`init`和`set`之间的区别。
英文:

For a quick example of the difference:

class C
{
    public int Prop1 { get; } = 2;
    public int Prop2 { get; init; } = 4;

    // constructor
    public C()
    {
        // Prop1 is only assignable here
        Prop1 = 6;
        // Prop2 is assignable here, but also in the "object initializer"
        Prop2 = 8;
    }
}

// the stuff inside the {} is what the docs are referring to
// as "object initializer"
var c = new C()
{
    // this next line would be an error
    // Prop1 = 10,

    // setting Prop2 works fine,
    // this also overwrites what was set in the constructor 
    Prop2 = 10
}

Prop2 cannot be set again after those {} end, which is what differentiates init from set.

huangapple
  • 本文由 发表于 2023年7月20日 08:50:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定