Best Practice For Class Property Initialization

转载请保留此原文链接 http://www.cnblogs.com/LikeVirgo/p/5126929.html

属性初始化的最佳实践

属性的初始化

初始化属性跟初始化字段大体是一样的。通常情况下,我们都是基于字段来实现属性,所以为字段设置好初始值就等同于为属性也设置好了。

 1 namespace PropertyInitialization
 2 {
 3     class User
 4     {
 5         private string name = "";
 6
 7         public string Name
 8         {
 9             get { return this.name; }
10             set { this.name = value; }
11         }
12
13         public User(string name)
14         {
15             this.Name = name;
16         }
17     }
18 }

自动实现的属性的初始化

对于自动实现的属性(Auto-Implemented Properties),在C# 6及更高的版本上,可以使用Auto-Property Initializers进行初始化。

 1 namespace PropertyInitialization
 2 {
 3     class User
 4     {
 5         public string Name { get; set; } = "";
 6
 7         public User(string name)
 8         {
 9             this.Name = name;
10         }
11     }
12 }

但使用C# 5时,就只能在构造函数中完成初始化了。

 1 namespace PropertyInitialization
 2 {
 3     class User
 4     {
 5         public string Name { get; set; }
 6
 7         public User()
 8         {
 9             this.Name = "";
10         }
11
12         public User(string name)
13         {
14             this.Name = name;
15         }
16     }
17 }

参考资料

  1. 字段初始化的最佳实践
  2. How do you give a C# Auto-Property a default value?
时间: 2024-10-10 18:26:14

Best Practice For Class Property Initialization的相关文章

Best Practice For Class Field Initialization

转载请保留此原文链接 http://www.cnblogs.com/LikeVirgo/p/5103308.html 正文 类的字段初始化参考下面的规则: 仅声明字段,让系统自动将其初始化为默认值. 非默认值的情况,优先选择variable initializer进行字段初始化. 使用带参数的构造函数,并在构造函数内完成字段初始化. 为什么要优先选择variable initializer(规则2)? 不论字段是否设置了variable initializer,在类初始化的时候,所有的字段都会先

Lazy Initialization with Swift

Lazy initialization (also sometimes called lazy instantiation, or lazy loading) is a technique for delaying the creation of an object or some other expensive process until it's needed. When programming for iOS, this is helpful to make sure you utiliz

Programming with Objective-C 学习笔记

这几天把Programming with Objective-C再看了一边,发现有很多以前不明确的地方.现在把一些重要的点记下来,巩固学习效果. 1.Objective-C Classes Are also Objects(OC类也是对象) In Objective-C, a class is itself an object with an opaque type called Class. Classes can't have properties defined using the dec

QML Object Attributes

Every QML object type has a defined set of attributes. Each instance of an object type is created with the set of attributes that have been defined for that object type. There are several different kinds of attributes which can be specified, which ar

swift 延迟加载

iOS Developer Lazy Initialization with Swift 22 Jun 2014 ? ∞       Lazy initialization (also sometimes called lazy instantiation, or  lazy loading) is a technique for delaying the creation of an object or  some other expensive process until it's need

学习笔记--数据库开发

//JDBC基础 JDBC URL jdbc:mysql://<ip>:<port>/database jdbc:oracle:thin:@<ip>:<port>:database jdbc:microsoft:sqlserver://<ip>:<port>; DatabaseName=database 常用方法: Statement stmt = conn.createStatement(); ResultSet rs = stmt

《Microsoft_Press_eBook_Xamarin_Preview_2_PDF》中文摘要(持续更新中)

内容来自谷歌翻译+个人理解,因联系不上原作者,完全翻译有可能涉及版权,所以摘录一些精要. 方便自己和他人交流参考学习,期待大家一块翻译剩下的(排版没来得及折腾,见谅). 环境:破解版Xamarin for Visual Studio 3.9.547,点我查看下载地址 (备注:只有windows版本,没有相应mac破解,不过对于学习来说足够了) 第一章:xamarin.forms适合什么情况? 1.oc,java,c#都是起源于c,所以有共同的地方. (These three languages

C# 6.0 新功能Top 10

http://www.developer.com/net/csharp/top-10-c-6.0-language-features.html 1,单行函数表达式化 用=>简化只有一行的函数. class Employee { // Method with only the expression public static int CalculateMonthlyPay(int dailyWage) => dailyWage * 30; } 2,?--条件 运算符 以前需要显式地写判断非空的条

TypeScript 上手教程

无疑,对于大型项目来说,Vanilla Js 无法满足工程需求.早在 2016 年 Anuglar 在项目中引入 TypeScript 时,大概也是考虑到强类型约束对于大型工程的必要性,具体选型考虑可参考这篇文章.然后可以看到 TypeScript 在社区中逐渐升温.但凡社区中举足轻重的库,如果不是原生使用 TypeScript 编写,那么也是通过声明文件的方式对 TypeScript 提供支持,比如 React(虽然不是包含在官方仓库中,而是通过 @types/react),同时官方脚手架工具