浅尝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)

     

 

If you’re still creating public variables in your
types, stop now. If you’re still creating get and set methods by hand, stop
now. Properties let you expose data members as part of your public interface
and still provide the encapsulation you want in an object-oriented
environment. Properties are language elements that are accessed as though
they are data members, but they are implemented as methods.

转载于:https://www.cnblogs.com/TivonStone/archive/2010/04/24/1719566.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/435577.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

WINCE驱动编写

一般我们在驱动程序中需用LocalAlloc保留一块存储空间,然后用LocalCopy将I/O端口映射到该地址上,以后就可以访问该保留下来的地址了。 而对中断的处理各种设备不尽相同。如果是一个内置设备(Built In),一般在oalintr.h…

【转】DICOM文件格式剖析(初识)

转自:DICOM文件格式剖析(初识)_MoreThinker的博客-CSDN博客_dicom格式 初识DICOM(适合初学者) 初识DICOM文件,发现网上的资料有点少,大部分的资料都不全,在这里做一下总结&#xf…

SVN各个状态:A C D M G U R I具体含义

svn status命令查看svn状态 ?:不在svn的控制中 A:add,新增 C:conflict,冲突; tc以他们改得为准 D:delete,删除 M:modify,本地已经修改 G:modify and merGed…

WINCE Driver 心得总结

一. Windows CE的驱动程序的区分 1.从加载以及接口方式来区分 可以分为本机设备驱动(Built-In Driver)、可加载驱动(Loadable Driver)以及混合型驱动。 (1)本机设备驱动 本机设备驱动…

【转】进阶 JavaScript 必知的 33 个点【进阶必备】

转自:进阶 JavaScript 必知的 33 个点【进阶必备】 进阶 JavaScript 必知的 33 个点【进阶必备】 Original 前端小菜鸡之菜鸡互啄 前端开发爱好者 2022-04-11 08:32 收录于话题#javaScript进阶1个 点击下方“前端开发爱好者”,选择“设为星标” 第一…

Struts配置

Struts应用采用两个基于XML的配置文件来配置,分别是web.xml和struts-cofig.xml文件。web.xml文件是配置所有web应用的而struts-config.xml文件是struts专用的配置文件,事实上也是可以根据需要给这个配置文件起其他名称的. Web应用的发布描述文件:web应用发布描述文件…

【转】开发者需要了解的领域特定语言(DSL)

转自:开发者需要了解的领域特定语言(DSL) - 知乎 领域特定语言是在特定领域下用于特定上下文的语言。作为开发者,很有必要了解领域特定语言的含义,以及为什么要使用特定领域语言。 领域特定语言domain-specific lang…

POJ 2240题(Floyd)

//使用Floyd的变形实现//这就是个套汇的问题&#xff0c;可以用Floyd求最大环&#xff0c;然后判断是不是大于1。#include <cstdio>#include <string>#include <map>using namespace std;map<string,int> MAP;double value[31][31];double rate;double…

【转】自动化构建、自动化部署发布一览

转自&#xff1a;自动化构建、自动化部署发布一览 - 知乎 在软件系统开发的过程中&#xff0c;一个项目工程通常会包含很多的代码文件、配置文件、第三方文件、图片、样式文件等等&#xff0c;是如何将这些文件有效的组装起来最终形成一个可以流畅使用的应用程序的呢&#xff…

承博士:让云计算落地生根的中国云计算平台

2010-01-22 09:48:41 [0评论 ] 金蝶中间件秉承自主创新&#xff0c;努力打造自主知识产权的云计算平台;金蝶Apusic云计算解决方案是为大型企业和组织提供私有云的解决方案&#xff0c;通过云计算中间件平台&#xff0c;为云计算提供落地的务实价值。金蝶中间件与国际厂商合作&…

【转】g++以及gcc的区别

转自&#xff1a;g以及gcc的区别 - 知乎 GCC &#xff0c;gcc 和g&#xff1a; 一直没搞清这几个东西的概念&#xff0c;搜了半天看到了一个不错的解释&#xff0c;所以大致记录一下&#xff0c;以免以后再忘记&#xff0c;链接。&#xff08;原谅没找到原文出处&#xff09;…

Microsoft Windows CE .NET 中的中断体系结构

概述 通过 Microsoft Windows CE .NET&#xff0c;Microsoft 已经升级了 Windows CE 的中断体系结构。该操作系统 (OS) 所具有的处理共享中断的能力极大地扩展了 Windows CE .NET 支持许多中断体系结构的能力。本文从原始设备制造商 (OEM) 和应用程序开发人员的角度探讨了处理…

微软企业库5.0学习笔记(三十三)数据访问模块

前言 鉴于企业库5.0已经发布正式版&#xff0c;同时有广大读者的要求&#xff08;臭屁一下&#xff0c;o(∩_∩)o...&#xff09;&#xff0c;后面文章的内容和代码将基于Enterprise Library5.0和Unity2.0来写&#xff0c;感谢大家的一贯支持。 正文 数据库访问模块都能实现哪些…

ARM中断分析之一:中断控制器和CPU、外设的关系

“中断控制器”也是CPU众多外设中的一个&#xff0c;不同的是&#xff0c;它一方面接收其它外设中断引脚的输入&#xff0c;另一方面&#xff0c;它会发出中断信号给CPU。下图是一张中断控制器外设的框图&#xff0c;s3c2410的框图。 为了把中断控制器、CPU、外设联系起来&…

【转】Jenkins项目常用三种构建类型风格详解

转自&#xff1a;Jenkins项目常用三种构建类型风格详解_ぃ小小宇宙的博客-CSDN博客_jenkins项目类型 Jenkins构建的项目类型介绍 jenkins 的安装配置请参考&#xff1a;《jenkins war包安装部署&#xff0c;tomcatJDKmaven》 Jenkins中自动构建项目的类型有很多&#xff0c;…

ARM中断分析之二:裸机下面的中断处理

EINT4中断的裸机处理 这是基于S3C2410的EINT4中断的裸机处理&#xff0c;当中断发生时就把LED灯取反显示。下面是电路图。 上面是外部KEY连接到CPU的EINT4引脚上面&#xff0c;即&#xff1a;按下键就会产生一个中断。 按照先前介绍的&#xff0c;中断处理流程来介绍&#xff0…

单节点hadoop部署成功

经过前面的配置&#xff1a; linux-ot1w:/home/macula/download/hadoop-0.20.2 # bin/start-all.sh starting namenode, logging to /home/macula/download/hadoop-0.20.2/bin/../logs/hadoop-macula-namenode-linux-ot1w.out Password: localhost: starting datanode, logging…

【转】Jenkins 构建触发器操作详解

转自&#xff1a;Jenkins 构建触发器操作详解 - 习久性成 - 博客园 前言 触发远程构建 【https://www.cnblogs.com/Rocky_/p/8297260.html】 例如&#xff0c;使用脚本&#xff1a;通过一个网址的访问来触发构建&#xff0c;这样就不需要登录jenkins系统也能触发构建了。 示…

ARM中断分析之三:WinCE驱动的中断分析

现在有许多高端的ARM芯片&#xff0c;像苹果、三星、华为都采用ARM芯片做为智能手机芯片。 这篇文章介绍基于ARM的WinCE操作系统的驱动的中断分析。WinCE驱动分为两类&#xff0c;这里介绍流驱动&#xff0c;流驱动比较简单。 关于流驱动&#xff0c;下面是一张框图&#xff0c…

【转】密码破解全能工具:Hashcat密码破解攻略

转自&#xff1a;密码破解全能工具&#xff1a;Hashcat密码破解攻略 - FreeBuf网络安全行业门户 Hashcat密码破解 hashcat号称世界上最快的密码破解&#xff0c;世界上第一个和唯一的基于GPGPU规则引擎&#xff0c;免费多GPU&#xff08;高达128个GPU&#xff09;&#xff0c…