房产(无额外的处理)与公共领域 [英] Property(with no extra processing) vs public field

查看:86
本文介绍了房产(无额外的处理)与公共领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当有关于属性的信誉问题,我看到大部分的讨论情况各地函数/方法VS属性。但我也想知道在引人注目之所以用财产相关的私有字段VS公共领域,直接本身,柜面最常见的get / set的行为,没有其他的处理,我的意思是这样

 公共字符串客户名称;

VS

 私人字符串客户名称;
公共字符串客户名称
{
{返回客户名称;}
集(字符串值){this.customerName =值;}
}


解决方案

您获得源/二进制兼容性,如果您以后需要添加其他的行为,你能添加破发点,并且它是关于行为只是哲学清洁剂(护理,不存储机制)。

请注意,你不需要整后者块的C#3:

 公共字符串客户名称{搞定;组; }

请参阅我的文章为什么物质属性了解详情。

Whenever there is question about credibility of Properties, I see that most of the discussion happens around functions/methods vs properties. But I would also like to know the compelling reason to use property with associated private field vs public field directly itself, incase of most common get/set behaviors with no other processing, I mean this way

public string CustomerName;

vs

private string customerName;
public string CustomerName
{
get{return customerName;}
set(string value){this.customerName=value;}
}

解决方案

You get source/binary compatibility if you later need to add other behavior, you get to add break points, and it's just philosophically cleaner (care about the behavior, not the storage mechanism).

Note that you don't need the whole of the latter block in C# 3:

public string CustomerName { get; set; }

See my article on "Why Properties Matter" for more information.

这篇关于房产(无额外的处理)与公共领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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