C#的保护属性或字段 [英] C# protected property or field

查看:193
本文介绍了C#的保护属性或字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你觉得这是更好地总是受保护的类成员自动实现保护的财产,以保持隔离或使其受保护字段是够吗?



 保护布尔测试{搞定;组; } 

 保护布尔测试; 


解决方案

通常情况下,你应该使用autoproperties - 这让你轻松添加验证,或其他任何你需要稍后。这一点尤其重要,如果受保护的成员将由类程序集外使用,如添加这样的代码不会破坏你与他们的合同,而更改一个字段的方法或将财产。


Do you think it's better to always make protected class members an auto-implemented protected property to keep isolation or make it protected field is enough?

protected bool test { get; set; }

or

protected bool test;

解决方案

Generally, you should use autoproperties - this allow you to easily add verification, or anything else you need later on. This is especially important if the protected member will be used by classes outside your assembly, as adding such code won't break your contract with them, whereas changing a field to a method or property will.

这篇关于C#的保护属性或字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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