C#中:什么是具有纯粹的公共属性点 [英] c#: what is the point in having a purely public property

查看:168
本文介绍了C#中:什么是具有纯粹的公共属性点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能显示的文件:结果
我应该使用公共属性和私有字段或对数据的公共字段?结果
Property(with没有额外的处理)与公共领域

这是在具有类简单地得到,并设置一个成员变量

What is the point in having a property inside a class that simply get and sets a member variable?

什么实际的差异将在那里只是让变量公开?

What practical difference would be there to just making the variable public?

推荐答案

我觉得类似的问题曾多次被回答,但基本上它可以让你添加错误检查的/ etc验证你的财产不改变公共接口的选项。

I think similar questions have been answered on many occasions, but basically it gives you the option of adding error checking/etc validation to your property without changing the public interface.

还有一些好消息这里,下面的报价可能回答你的问题最好的:

There's also some good information here, the following quote probably answers your question best:

一个物业沟通的想法的我会向您提供一个值,或者从你接受的价值。这不是一个实现概念,它是一个接口的概念。一个字段,而另一方面,通讯的实现 - 它说:这种类型的在这个非常特殊的方式代表值。有没有封装,它的裸存储格式。这其中的道理字段不是接口部分的一部分 - 他们不属于那里,因为他们谈是如何实现的,而不是被取得的成就。

A property communicates the idea of "I will make a value available to you, or accept a value from you." It's not an implementation concept, it's an interface concept. A field, on the other hand, communicates the implementation - it says "this type represents a value in this very specific way". There's no encapsulation, it's the bare storage format. This is part of the reason fields aren't part of interfaces - they don't belong there, as they talk about how something is achieved rather than what is achieved.

这篇关于C#中:什么是具有纯粹的公共属性点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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