属性 vs. 字段:需要帮助掌握属性在字段上的使用 [英] Properties vs. Fields: Need help grasping the uses of Properties over Fields

查看:27
本文介绍了属性 vs. 字段:需要帮助掌握属性在字段上的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已经阅读了有关此主题的帖子列表,但由于我对封装和字段修饰符(private、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# 的主要方面之一是通过使用封装在代码中保护数据的重要性.我认为"我理解这是因为使用修饰符(私有、公共、内部、受保护)的能力.然而,在了解了属性之后,我不仅对属性的使用,而且对 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,在我看来,除非您有理由更改类型(#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) 为什么我想使用属性而不是字段(尤其是当我只是添加额外的代码时

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

2) 在跟踪其他人的代码时,关于识别属性的使用而不是将它们视为简单的方法(get;set 除外)的任何提示?

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?

感谢并抱歉发了这么长的帖子——我不想只问一个被问了 100 倍的问题,而没有解释我为什么再次问这个问题.

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.

推荐答案

您不必担心通过属性访问字段所需的额外代码,它会被 JIT 编译器优化"掉(通过内联代码).除非它太大而无法内联,但无论如何您都需要额外的代码.

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.

而且用于定义简单属性的额外代码也很少:

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.

我的规则:始终通过属性公开字段

这篇关于属性 vs. 字段:需要帮助掌握属性在字段上的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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