物业后盾值范围 [英] Property backing value scope

查看:97
本文介绍了物业后盾值范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是这样子可能什么?我猜想不是,但它的看起来不错的对我说:

Is anything like this possible? I'm assuming not, but it looks good to me:

class MyClass {
    public int Foo {
        get { return m_foo; }
        set {
            // Bounds checking, or other things that prevent the use
            // of an auto-implemented property
            m_foo = value;
        }

        // Put the backing field actually *in* the scope of the property
        // so that the rest of the class cannot access it.
        private int m_foo;
    }

    void Method() {
        m_foo = 42;    // Can't touch this!
    }
}



我当然知道这个语法是不正确的,这将无法编译。这是假设性的,未来的C#进行的描绘清楚我的想法的缘故。我对有些假设性的问题道歉,但它太具体的Programmers.SE

Of course I know this syntax is incorrect, and this will not compile. It was hypothetical-future-C# for the sake of clearly portraying my idea. I apologize for the somewhat hypothetical question, but it is too specific for Programmers.SE.

像这样的东西可以在这将成为一个目的编译器来实现:只允许酒店的 GET 设置访问到实地看,基本上是允许的财产,以自成体系(如自动实现属性),同时允许更多的get / set逻辑。

Something like this could be implemented in the compiler that would serve one purpose: Only allow the property's get and set accessors to see the field, essentially allowing the property to be self-contained (as auto-implemented properties are) while allowing additional get/set logic.

推荐答案

简短的回答是否定的,今天是不可能在C#中。

The short answer is no, that's not possible in C# today.

我们得到这样一个功能要求相当频繁;它在其更一般的形式一个很好的功能。更一般的形式是为了更清楚地做出的寿命的的的本地的变量垂直于其的范围

We get a feature request like this fairly often; it's a nice feature in its more general form. The more general form is to more clearly make the lifetime of a local variable orthogonal to its scope.

只是为了确保这些条款是明确的:一个的变量的是一个存储位置,可能命名。每个变量具有的寿命的:时间在运行时,其中变量是保证指有效存储量。在范围的一个的名称的是可以使用该名称的文本区域;它是一个编译时间的概念,而不是一个运行时的概念。 A 本地的变量是一个变量,其的范围的是语句块

Just to make sure those terms are clear: a variable is a storage location, possibly named. Every variable has a lifetime: the amount of time at runtime in which the variable is guaranteed to refer to valid storage. The scope of a name is the region of text in which that name may be used; it is a compile-time concept, not a runtime concept. A local variable is a variable whose scope is a statement block.

在许多语言中,一个局部变量的生存期密切相关的范围:在控制逻辑输入范围在运行时,生命周期开始,当它离开的范围,寿命结束。这是在C#中真正具有一些显着的注意事项:

In many languages, the lifetime of a local variable is closely tied to its scope: when control logically enters the scope at runtime, the lifetime begins and when it leaves the scope, the lifetime ends. This is true in C# with some notable caveats:


  • 本地的寿命可以延长或截断如果运行时能够确定这样做并没有什么影响,以托管代码的动作的在当前线程上的。其他线程在当前线程上的操作(例如,终结器线程)和非托管代码是实现定义的。

  • The lifetime of a local may be extended or truncated if the runtime can determine that doing so has no consequence to the action of managed code on the current thread. The actions of other threads (like the finalizer thread) and unmanaged code on the current thread are implementation-defined.

地方是在的生命周期迭代器块,异步方法,或封闭在一个匿名函数的外层变量,可以延长达到或超过使用它的迭代器,任务委托或表达式树的寿命。

The lifetime of a local that is in an iterator block, an async method, or a closed-over outer variable of an anonymous function, may be extended to match or exceed the lifetime of the iterator, task, delegate or expression tree that uses it.

显然,这不是一个的需求的一个本地的寿命和范围的任何方式连接在一起。这将是很好,如果我们可以明确地有当地人有一个实例或静态字段的寿命,但局部的范围。 C有此功能;你可以做一个静态的局部变量。 C#不。您的建议是基本上以允许具有实例的生命周期的属性的块内的局部变量,但其范围被限制在块

Clearly it is not a requirement that the lifetime and scope of a local be tied together in any way. It would be nice if we could explicitly have locals that have the lifetime of an instance or static field, but the scope of a local. C has this feature; you can make a "static" local variable. C# does not. Your proposal is essentially to allow a local variable within the block of the property that has the lifetime of the instance but whose scope is restricted to the block.

我归类这功能,因为好。我们有潜在的好的功能列表字面上只要你的手臂,我们没有时间来实现,所以我不希望这样一来使其向列表的顶部任何时间很快。感谢尽管反馈;它可以帮助我们有点优先处理名单。

I would classify this feature as "nice". We have a list of potential "nice" features literally as long as your arm that we don't have time to implement, so I wouldn't expect this one to make it to the top of the list any time soon. Thanks for the feedback though; it helps us prioritize that list somewhat.

这篇关于物业后盾值范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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