是什么在C#`Fields`和`Properties`之间的区别? [英] What is the difference between `Fields` and `Properties` in C#?

查看:1381
本文介绍了是什么在C#`Fields`和`Properties`之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑,根据这些评论:

你的意思是财产与场?
公共字符串S1; VS公共字符串S2
{搞定;组; } - 达纳

Do you mean "Property" vs "Field"? public String S1; vs public String S2 { get; set; } – dana

完全布施,我的意思是一样的。 - 阿萨德

Exactly dana, i mean the same. – Asad

阿萨德:你真的需要尝试使用
其他一些词来形容你
是什么意思,使我们能够更好地理解
你的问题。 C#没有全局
变量。你可以在C#中定义
中的字段不是全局性的 - 他们是类类型的
的成员。 - dthorpe

Asad: you really need to try to use some other term to describe what you mean so that we can better understand your question. C# does not have global variables. The fields you can define in C# are not global - they are members of the class type. – dthorpe

喜伙计们,

需要在字段之间的区别你的专家意见属性。正如在我的项目,我已经使用它后来我改成'属性'某些全局变量。我的经理问什么是使用的变量,而不是字段 属性的好处。

Need your expert views over the difference between Field and Property. As in my project, I have used certain global variables which later on i changed to 'Properties' . My manager is asking what is the benefit of using Properties of variables instead of Fields.

虽然我回答他说,属性提供了一种安全/安全/间接访问字段,而不是直接修改他们,如果他们被声明公共保护。但请给我提供一些更多的有说服力的证据。

Although I have replied him that Property provides a kind of secure/safe/indirect access to Field instead of modifying them directly if they are declared public or protected. But Please provide me with some more convincing arguments.

感谢和问候

@Asad:
你应该让你的术语正确的:字段不是全局变量,C#没有全局变量(如一些评论者提到:你可以模拟全局变量,但你不应该这样做)

@Asad: You should get your terminology right: Fields are not Global Variables, C# does not have global variables (as a few commenters mentioned: you can simulate global variables, but you should not do that).

推荐答案

的主要优点是,你可以将所有各种各样的功能属性,如验证,同步等等。你不能这样做,一类领域。例如,一个字段可以扔在分配BCL异常,但它不能抛出一个异常,在你的问题域道理与逻辑。

The main advantage is that you can attach all sorts of functionality to a property such as validation, synchronization etc. You can't do that for a class field. For example, a field can throw BCL exceptions on assignment but it can't throw an exception that make sense with logic in your problem domain.

此外想象试图保护现场为线程同步。您在所有在你的代码所在的域被访问的地方,为编写额外的代码。要做到这一点有一个属性你可以简单地包裹getter和setter聚集在一处的锁。 (但要小心!放心的使用属性getter和setter锁可以给你安全的错觉,如果你与可变类型的工作。的见接受的答案在这个岗位。

Also imagine trying to protect a field for thread synchronization. You have to write extra code in all the places in your code where the field is accessed. To do that with a property you can simply wrap the getter and setter with a lock in one place. (But beware! The ease of using lock in property getters and setters can give you a false sense of security if you're working with mutable types. See the accepted answer in this post.)

现在,你可能会认为验证和同步都没有你很重要对于该特定值,并且它们可能永远此特定实例。但是,通过使用属性而不是直接字段访问是使你的应用程序多,在将来更易于维护。 (假设一个整数字段的值突然需要来自从原来的实现不同的来源,它需要从字符串到int转换。如果你使用属性来包装领域,那么你做一个地方的变化,所有使用该属性并不需要做任何改动客户端代码!)

Now, you might think that validation and synchronization are not important to you for this particular value, and they may never be for this particular instance. But by using properties instead of direct field access is making your application much more maintainable in the future. (Suppose the value of an integer field suddenly needs to come from a source different from the original implementation and it needs to be converted from a string to an int. If you use properties to wrap the field then you make the change in one place and all the client code that uses that property does not need to change at all!)

此外,管理)在许多类共享(全球信息看一看的< A HREF =http://en.wikipedia.org/wiki/Singleton_pattern相对=nofollow> Singleton模式。但要小心!尽管它看起来整洁干净,你仍然可以得到与它的麻烦。但如果你的真的需要全局数据,你应该使用包含在一个单独的属性。如果不出意外,这是一个很好的组织策略。

Also, for managing information shared across many classes (global) take a look at the singleton pattern. But beware! Even though it looks neat and clean you can still get into trouble with it. Though if you really need global data you should use properties contained in a singleton. If nothing else, it's a good organization strategy.

要避免单身的问题或全局的数据来看看的依赖注入作为一个更好的选择。

To avoid issues with singletons or "global" data take a look at dependency injection as a much better alternative.

这篇关于是什么在C#`Fields`和`Properties`之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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