简体   繁体   English

为什么在C#6.0自动属性初始化中没有`this`?

[英]Why is `this` not available in C# 6.0 Auto-Property Initialization?

I have the following code class: 我有以下代码类:

public class Foo
{
    public Nested Bar { get; } = new Nested(this);

    public class Nested
    {
        public Nested(Foo foo)
        {
            foo.DoSomething();
        }
    }

    private void DoSomething()
    {

    }
}

However, I get this compile error: 但是,我收到此编译错误:

Keyword 'this' is not available in the current context 关键字'this'在当前上下文中不可用

I can fix it by simply not using Auto-Property Initializer, and explicitly move it into a constructor instead: 我可以通过简单地不使用自动属性初始化器来修复它,并明确地将其移动到构造函数中:

public Nested Bar { get; }

public Foo()
{
    this.Bar = new Nested(this);
}

Why is it so? 为什么会这样? Isn't Auto-Property Initializer actually translated into constructor code in IL? 是不是Auto-Property Initializer实际上转换为IL中的构造函数代码?

Simply: you can't use this in initializers. 简单地说:你不能在初始化器中使用this The idea is to prevent an incomplete object from escaping - Nested(this) could do anything to your object, leading to very confusing and hard to understand bugs. 这个想法是为了防止一个不完整的对象逃逸 - Nested(this)可以对你的对象做任何事情 ,导致非常混乱和难以理解的错误。 Keep in mind that initializers execute before any constructor that you add. 请记住,初始化程序您添加的任何构造函数之前执行。 The same thing fails for field initializers too, in exactly the same way: 对于字段初始化器,同样的事情也是如此,完全相同:

private Nested _field = new Nested(this);

Essentially, initializers are intended to perform simple initializations - fixing the 98% problem. 本质上,初始化程序旨在执行简单的初始化 - 修复98%的问题。 Anything involving this is more complex, and you'll need to write your own constructor - and take the blame for any timing issues :) 涉及到this一点的任何事情都比较复杂,你需要编写自己的构造函数 - 并承担任何时间问题的责任:)

Why is it so? 为什么会这样? Isn't Auto-Property Initializer actually translated into constructor code in IL? 是不是Auto-Property Initializer实际上转换为IL中的构造函数代码?

The rules for automatically implemented property initializers are the same as those for field initializers, for the same reason. 出于同样的原因,自动实现的属性初始值设定项的规则与字段初始值设定项的规则相同。 Note that property initializers are executed before base class bodies, just like field initializers - so you're still in the context of a "somewhat uninitialized" object; 请注意,属性初始值设定项基类体之前执行,就像字段初始值设定项一样 - 因此您仍然处于“有点未初始化”对象的上下文中; more so than during a constructor body. 比在构造函数体中更多。

So you should imagine that the property is being converted into this: 所以你应该想象该属性正在转换为:

private readonly Nested bar = new Nested(this); // Invalid

public Nested Bar
{
    get { return bar; }
}

In short, this restriction is to stop you from getting yourself into trouble. 简而言之,这种限制是为了阻止你陷入麻烦。 If you need to refer to this when initializing a property, just do it manually in a constructor, as per your second example. 如果您需要参照this初始化属性时,只是做手工,在构造函数中,按你的第二个例子。 (It's relatively rare in my experience.) (根据我的经验,这是相对罕见的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中将auto-property初始化为非null? - How to initialize auto-property to not null in C#? C#访问器自动属性不足以实现FillPolygon() - C# Accessor auto-property not sufficient for FillPolygon() 当属性为类时,如何为C#自动属性赋予默认值 - How do you give a C# Auto-Property a default value When the Property is a Class 在C#中我可以通过属性帮助自动属性执行一些额外的工作吗? - In C# can I make auto-property perform some extra work with a help of an attribute? C#自定义自动属性设置器不允许我按摩值 - c# custom auto-property setter not allowing me to massage the value C#auto-property snippet将get和set放在新行上 - C# auto-property snippet puts get and set on new line 使用仅限getter的自动属性(C#6功能)显式实现接口 - Explicit implementation of an interface using a getter-only auto-property (C# 6 feature) 为 C# 自动属性赋予初始值的最佳方法是什么? - What is the best way to give a C# auto-property an initial value? 如何在DynamicProxy中为C#Auto-Property提供默认值? - How do you give a C# Auto-Property a default value in DynamicProxy? 接口定义内自动属性和方法的名称相同时,C#编译器错误 - C# Compiler Error when name of auto-property and a method is identical within interface definition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM