为什么要使用私有成员,然后使用公共属性来设置呢? [英] Why use private members then use public properties to set them?

查看:232
本文介绍了为什么要使用私有成员,然后使用公共属性来设置呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看代码的几个例子,发生这种情况:

Seen a few examples of code where this happens:

public class Foo
{
    string[] m_workID;
    public string[] WorkID
    {
        get
        {
            return m_workID;
        }
        private set
        {
            m_workID = value;
        }
    }
}



!这是什么意义呢?
因为使用m_workID unnescessary。

What's the point of this? Since the use m_workID unnescessary.

推荐答案

在一般情况下,关键是要分开的实施(场)从 API 的(财产)。

In general, the point is to separate implementation (the field) from API (the property).

后来你可以,如果您愿意,把逻辑,测井等在财产不破坏源或二进制兼容性 - 但更重要的是你说你的类型是什么愿意做,而不是它是如何打算这样做。

Later on you can, should you wish, put logic, logging etc in the property without breaking either source or binary compatibility - but more importantly you're saying what your type is willing to do, rather than how it's going to do it.

我有文章给使用属性,而不是公共领域的更多的好处。

I have an article giving more benefits of using properties instead of public fields.

在C#3你可以让这一切变得简单许多具有的自动实现的属性的:

In C# 3 you can make all of this a lot simpler with automatically implemented properties:

public class Foo
{
    public string[] WorkID { get; private set; }
}



在这一点上,你仍然有一个公共的getter和私人二传手,但为您生成支持字段(和财产执行)幕后。在任何时候,你可以改变这种与支持字段正常的全面实现的属性,你仍然有二进制和源代码兼容性。 (序列化对象的兼容性是另一回事,请注意。)

At that point you still have a public getter and a private setter, but the backing field (and property implementation) is generated for you behind the scenes. At any point you can change this to a "normal" fully-implemented property with a backing field, and you'll still have binary and source compatibility. (Compatibility of serialized objects is a different matter, mind you.)

此外,在这种情况下,你的无法的反映你想要的行为一个字段(公开阅读的价值,但它私下写的能力) - 你可以有一个只读域,但你可以的只有的构造函数中写入。我个人希望有这种类似的简写:

Additionally, in this case you can't mirror the behaviour you want (the ability to read the value publicly but write it privately) with a field - you could have a readonly field, but then you could only write to it within the constructor. Personally I wish there were a similar shorthand for this:

public class Foo
{
    private readonly int id;
    public int Id { get { return id; } }

    ...
}



我喜欢不可变的类型,但是这是一个不同的问题。

as I like immutable types, but that's a different matter.

另一个的不同的问题,它一般不暴露这样的阵列是一个好主意无论如何 - 甚至虽然呼叫者不能更改数组 WorkID 指的是,他们的可以的更改阵列,这可能不是你想要的内容。

In another different matter, it's generally not a good idea to expose arrays like this anyway - even though callers can't change which array WorkID refers to, they can change the contents of the array, which is probably not what you want.

在这个例子中你已经给你的可能的闪避没有属性setter,就直接设置在同一类中的字段,但是它会意思是如果你想添加记录等你必须找到所有的写操作。

In the example you've given you could get away without the property setter, just setting the field directly within the same class, but it would mean that if you ever wanted to add logging etc you'd have to find all those writes.

这篇关于为什么要使用私有成员,然后使用公共属性来设置呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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