C# getter/setter 幕后内部名称

发布于 2025-01-01 02:12:21 字数 735 浏览 0 评论 0原文

可能的重复:
我可以创建一个带有 get 和 set 代码的自动属性(无私有成员)?
访问自动属性 ​​- c#

我使用过显式 getters/setters,例如

private bool myField;
public bool MyField
{  get { return myField; }
   set { myField = value; }
}

Now,使用 C# .net 4.0,您可以进行缩写,例如

public bool MyField
{ get; set; }

现在,如果我只想覆盖 SET 部分,我应该引用的内部引用是什么...在第一个示例中,我知道我明确指的是“myField”的私有属性,但是对于第二个版本,我指的是什么呢?编译器是否只是抛出隐含的“_”(例如 _MyField)作为元素的私有端?

Possible Duplicate:
Can I create an automatic property (no private member) with get and set code?
Access automatic property - c#

I've worked with explicit getters / setters such as

private bool myField;
public bool MyField
{  get { return myField; }
   set { myField = value; }
}

Now, working with C# .net 4.0, you have the ability to abbreviate such as

public bool MyField
{ get; set; }

Now, if I want to override only the SET portion, what is the INTERNAL reference I should be referencing... in the first sample, I know I am explicitly referring to the private of "myField", but with the second version, what am I referencing? Does the compiler just throw an implied "_" such as _MyField as the private side of the element?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幻梦 2025-01-08 02:12:21
class Parent
    {
        public virtual bool MyField { get; set; }
    }

    class Child : Parent
    {
        public override bool MyField
        {
            //ommitting get portion
            set
            {
                //other custom code goes here
                base.MyField = value;
            }
        }
    }

这里,一个类继承了一个具有属性的类,并且仅覆盖了 setter。或者,您可以覆盖 getter 并将其保留为 return base.MyField 而不更改功能。

编辑:
提出的问题是,执行此操作时,子类中将不存在被遗漏的一半(我的示例中的 get ),从而使该属性只能读/写。事实并非如此,被遗漏的一半只是从其父级继承其功能。请参阅我的示例中添加的以下内容进行演示。

                Child c = new Child();
                c.MyField = true;
                if (c.MyField)
                {
                    Console.WriteLine("hi");
                }

(这确实会打印“hi”,编译或运行时没有错误。)

class Parent
    {
        public virtual bool MyField { get; set; }
    }

    class Child : Parent
    {
        public override bool MyField
        {
            //ommitting get portion
            set
            {
                //other custom code goes here
                base.MyField = value;
            }
        }
    }

Here a class inherits from a class with a property an overrides only the setter. Alternatively you could override the getter and just leave it as return base.MyField to not change the functionality.

Edit:
The issue was raised that when doing this the half that was left out (the get in my example) wouldn't exist in the child class, making the property read/write only. That is not the case, the half that is left out simply inherits its functionality from its parent. See the following addition to my example to demonstrate.

                Child c = new Child();
                c.MyField = true;
                if (c.MyField)
                {
                    Console.WriteLine("hi");
                }

(This will indeed print "hi", no errors compile or runtime.)

阿楠 2025-01-08 02:12:21

在这种情况下,编译器所做的一切都是实现细节,将来可能会更改,恕不另行通知!

因此,我强烈建议不要让您的代码依赖于这样的实现细节,而在这种情况下只使用第一个选项(覆盖两个访问器并具有字段来显式支持属性)...

whatever the compiler does in this case is an implementation detail which can change in the future without further notice!

Thus I strongly recommend to not make your code depend on such an implementation detail and just use the first option (override both accessors and have field to explicitely back the property) in this case...

复古式 2025-01-08 02:12:21

您可以使用 MyField 以相同的方式访问该属性。但是,如果您不希望类外部的代码能够设置该属性,您可以使用:

public bool MyField { get; private set; }

You would access the property in the same way, using MyField. However, if you did not want code outside the class to be able to set the property, you can use:

public bool MyField { get; private set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文