属性与字段:需要帮助过场抓性质的用途 [英] Properties vs. Fields: Need help grasping the uses of Properties over Fields

查看:96
本文介绍了属性与字段:需要帮助过场抓性质的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已通过关于这一主题的贴子列表看,我不觉得我已经掌握了,因为我已经体会到了关于封装和现场修饰符(私人,public..ect)属性。

First off, I have read through a list of postings on this topic and I don't feel I have grasped properties because of what I had come to understand about encapsulation and field modifiers (private, public..ect).

一个C#的,我是来学习的主要方面是数据保护您的code内使用封装的重要性。我'想'我明白,是因为使用了修饰符(私有,公共,内部保护)的能力。然而,学习有关属性后,我有点撕裂理解不仅性能使用,但数据保护的总体重要性/能力C#中(我理解为封装)。

One of the main aspects of C# that I have come to learn is the importance of data protection within your code by the use of encapsulation. I 'thought' I understood that to be because of the ability of the use of the modifiers (private, public, internal, protected). However, after learning about properties I am sort of torn in understanding not only properties uses, but the overall importance/ability of data protection (what I understood as encapsulation) within C#.

要更具体,一切我已阅读,当我在C#中得到了性的是,你应该尝试的地方领域使用它们时,你可以因为:

To be more specific, everything I have read when I got to properties in C# is that you should try to use them in place of fields when you can because of:

1)他们允许你改变时,你不能直接时候直接访问字段的数据类型。

1) they allow you to change the data type when you can't when directly accessing the field directly.

2)他们增加了一层保护数据访问

2) they add a level of protection to data access

不过,从我的'想'我已经来了解采用现场改性剂做#2,这在我看来,物业刚生成额外的code,除非你有一些理由来改变类型(# 1) - 因为你是创建隐藏的方法来访问领域的直接反对(或多或少)。

However, from what I 'thought' I had come to know about the use of field modifiers did #2, it seemed to me that properties just generated additional code unless you had some reason to change the type (#1) - because you are (more or less) creating hidden methods to access fields as opposed to directly.

然后是整个改性剂能够被添加到我的理解进一步复杂化的必要性的属性的访问数据属性

Then there is the whole modifiers being able to be added to Properties which further complicates my understanding for the need of properties to access data.

我已阅读属性许多来自不同作家的章节,并没有真正地解释性质的一个很好的理解与对封装字段(以及良好的编程方法)。

I have read a number of chapters from different writers on "properties" and none have really explained a good understanding of properties vs. fields vs. encapsulation (and good programming methods).

有人能解释:

1)为什么我会想使用属性,而不是领域(特别是当它出现时我只是添加额外的code

1) why I would want to use properties instead of fields (especially when it appears I am just adding additional code

2)在承认使用特性,而不是把它们看作简单的方法(用get除外任何提示;追踪其他国家的人民code,当设置为明显)

2) any tips on recognizing the use of properties and not seeing them as simply methods (with the exception of the get;set being apparent) when tracing other peoples code?

3)拇指的一般规则,当谈到关于何时使用什么好编程方法?

3) Any general rules of thumb when it comes to good programming methods in relation to when to use what?

感谢,并为长期职位对不起 - 我不想只是要求已经提出100X没有解释为什么我再次要求它的问题。

Thanks and sorry for the long post - I didn't want to just ask a question that has been asked 100x without explaining why I am asking it again.

推荐答案

您不应该担心需要通过属性访问字段额外的code,它会被优化掉了JIT编译器(由内联code)。除非是太大,内联,但你需要额外的code无妨。

You should not worry about the extra code needed for accessing fields via properties, it will be "optimized" away by the JIT compiler (by inlining the code). Except when it is too large to be inlined, but then you needed the extra code anyway.

和额外的code定义简单的属性也是最少的:

And the extra code for defining simple properties is also minimal:

public int MyProp { get; set; } // use auto generated field.

当你需要的自定义的你可以送花儿给人定义自己的后场

When you need to customize you can alway define your own field later.

所以,你留下的封装/数据保护的附加层,这是一件好事。

So you are left with the extra layer of encapsulation / data protection, and that is a good thing.

我的规则:通过属性总是暴露领域

这篇关于属性与字段:需要帮助过场抓性质的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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