浅尝EffectiveCSharp_1
Item 1: 使用属性,避免可访问的数据成员 Use Properties Instead of Accessible Data Members
- 属性允许你创建一个想可访问数据的接口,而且仍然有使用方法的所有优点.Properties enable you to create an interface that acts like data access but still has all the benefits of a method.在.NET框架设计中数据绑定支持属性,而不是公共数据成员,在WPF,WF,WebForms,Silverlight中是一样的.In fact, the data binding classes in the .NET Framework support properties, not public data members. This is true for all the data binding libraries: WPF, Windows Forms, Web Forms, and Silverlight.
- textBoxCity.DataBindings.Add("Text",address, "City");
- 属性更易于更改,可以做校验或者额外的验证Properties are far easier to change as you discover new requirements or behaviors over time. You might soon decide that your customer type should never have a blank name. If you used a public property for Name, that’s easy to fix in one location
1 public class Customer
2 {
3 private string name;
4 public string Name
5 {
6 get { return name; }
7 set
8 {
9 if (string.IsNullOrEmpty(value))
10 throw new ArgumentException( "Name cannot be blank", "Name");
11 name = value;
12 }
13 }
14 } - 因为属性里可以使用方法,因此添加多线程变得更加容易.Because properties are implemented with methods, adding multithreaded support is easier. You can enhance the implementation of the get and set accessors to provide synchronized access to the data:
1 public class Customer
2 {
3 private object syncHandle = new object();
4 private string name;
5 public string Name
6 {
7 get
8 {
9 lock (syncHandle)
10 return name;
11 }
12 set
13 {
14 if (string.IsNullOrEmpty(value))
15 throw new ArgumentException("Name cannot be blank","Name");
16 lock (syncHandle)
17 name = value;
18 }
19 }
20 } - 属性也可以是虚的,或者是抽象的.Properties have all the language features of methods. Properties can be virtual:
1 public class Customer
2 {
3 public virtual string Name { get; set; }
4 }这是C#3.0隐式声明语法.
- 你可以对get和set两个属性访问器做任何的修改,这比单单定义数据可见性强大的多.You can specify different accessibility modifiers to the get and set accessors in a property in C#. This gives you even greater control over the visibility of those data elements you expose as properties,这里也可以提供get-only或者set-only版本,甚至可以给读、写以不同的访问权限
1 public class Customer
2 {
3 public virtual string Name
4 {
5 get;
6 protected set;
7 }
8 }
Item 2: 使用只读方式,避免使用常量声明 Prefer readonly to const
- C#有两种常量版本,一种是编译时常量,另一种是运行时常量.错误的运用会导致性能和正确性降低.C# has two different versions of constants: compile-time constants and runtime constants. They have very different behaviors, and using the wrong one will cost you performance or correctness. 编译时常量比运行时常量稍微快点,但是灵活性差很多Compile-time constants are slightly faster, but far less flexible, than runtime constants
- 用readonly声明运行时常量,const声明编译时常量You declare runtime constants with the readonly keyword. Compile-time constants are declared with the const keyword.
- 编译时常量(const)可以在方法声明,运行时常量(readonly)不能在方法范围内声明Compile-time constants can also be declared inside methods. Read-only constants cannot be declared with method scope.
- 编译时常量被限制为数字,字符串类型.而运行时常量则可以是任何类型Compile-time constants are limited to numbers and strings. Read-only values are also constants, in that they cannot be modified after the constructor has executed. But read-only values are different in that they are assigned at runtime. You have much more flexibility in working with runtime constants. For one thing, runtime constants can be any type.
- 编译时常量与运行时常量行为的不同处在于它们的访问方式。编译时常量在编译后的结果代码中会被替换为该常量的值,例如下面的代码The differences in the behavior of compile-time and runtime constants follow from how they are accessed. A compile-time constant is replaced with the value of that constant in your object code. This construct:
if (myDateTime.Year == Millennium)
compiles to the same IL as if you had written this:
if (myDateTime.Year == 2000)
转载于:https://www.cnblogs.com/TivonStone/archive/2010/04/24/1719566.html
浅尝EffectiveCSharp_1相关推荐
- 浅尝key-value数据库(三)——MongoDB的分布式
浅尝key-value数据库(三)--MongoDB的分布式 测试了单机MongoDB的随机读和写入性能,这一节来讲一讲MongoDB的分布式. MongoDB的分布式分成两种,一种是Replicat ...
- 论文浅尝 | 利用多语言 wordnet 上随机游走实现双语 embeddings
论文笔记整理:谭亦鸣,东南大学博士生,研究方向为知识图谱问答. 来源:Knowledge Based System 链接:https://www.sciencedirect.com/science/a ...
- 论文浅尝 | 图神经网络综述:方法及应用
论文链接:https://arxiv.org/pdf/1812.08434.pdf GNN相关论文列表链接:https://github.com/thunlp/GNNPapers 近日,清华刘知远老师 ...
- 论文浅尝 | 当知识图谱遇上零样本学习——零样本学习综述
随着监督学习在机器学习领域取得的巨大发展,如何减少人工在样本方面的处理工作,以及如何使模型快速适应层出不穷的新样本,成为亟待解决的问题.零样本学习(Zero-Shot Learning, ZSL)的提 ...
- 论文浅尝 | 近期论文精选
本文转载自公众号 PaperWeekly, 对我们近期的论文浅尝进行了精选整理并附上了相应的源码链接,感谢 PaperWeekly! TheWebConf 2018 ■ 链接 | https://ww ...
- 论文浅尝 | 从 6 篇顶会论文看「知识图谱」领域最新研究进展 | 解读 代码
本文内容源自往期「论文浅尝」,由 PaperWeekly 精选并重新排版整理,感谢 PaperWeekly. ISWC 2018 ■ 链接 | http://www.paperweekly.site/ ...
- 通过集成腾讯 IM 来浅尝一下.net 6 的 MINI API
背景 下一篇在继续 Go 的学习笔记,因为这阵子一直有项目压着,确实没有精力去总结学习成果.所以这篇就先换换口味,切回老本行,分享一下.net 6 的一个小知识. 前些天,我们对外提供的一些管理系统, ...
- 浅尝Pytorch自动混合精度AMP
AMP目录 浅尝Pytorch自动混合精度 从浮点数说起 深度学习中的浮点数 例1-上溢 例2-下溢 解决了什么问题? Pytorch相关功能简述 Autocasting Autocasting作上下 ...
- bo耳机h5使用说明_给想浅尝便携hifi耳机的烧友的一点建议
初入门的烧友,要么是好友推荐品牌或型号,要么是机缘巧合蹭听到了好耳机的声音,心潮澎湃,仿佛一下打开了新世界的大门!原来听的歌,现在听感觉完全不同,有种豁然开朗的感觉,之后,心如小鹿乱撞,恨不得马上买买 ...
最新文章
- 人机协同作战:或改写未来战争规则
- hadoop集群时间同步
- nginx用户认证访问
- 详解spark任务提交至yarn的集群和客户端模式
- TensorFlow错误:TypeError: __init__() got an unexpected keyword argument 'serialized_options'
- 【嵌入式】Libmodbus之TCP模式Slave端程序示例
- linux中断响应时间太慢_linux+arm系统学习与基础学习
- java的或等于_Java中的“小于或等于”比较运算符是__________: !=|||=|=
- 开始android旅程
- c++程序设计中多态与虚函数知识点
- Windows环境下JDK安装与环境变量配置详细的图文教程
- Linux Redis安装及使用
- Ubuntu18.04有线+离线划词翻译GoldenDict
- linux 一次对一个用户限制存取
- 【附源码】计算机毕业设计java兴澜幼儿园管理系统设计与实现
- 啊哈C语言第四章 第九节
- ATtiny85单片机制作PCB小提琴
- Whois查询结果中不同域名状态的含义
- 爱心代码(带字的奥)
- 算命大师元真先生解读周易人生命运
热门文章
- java线程并发库之--线程同步工具Exchanger的使用
- Aptana插件安装到eclipse和myeclipse的详细过程
- A→CALL→B时防止B程序COMMIT掉A程序文件的方法
- 一个c++ 2d图形引擎 AGG
- java xml导出_java 导出xml文件的四种方式
- 我为什么会选择计算机专业之 《我的编程人生前传》
- o.s.b.d.LoggingFailureAnalysisReporter
- common lisp 学习第一天 初步接触
- php额拍戏,像这种会演戏的演员,给我焊在剧组365天拍戏可以吗?
- 【C语言进阶深度学习记录】三 浮点数(float) 在内存中的表示方法