Automatic Properties in C#
来源:互联网 发布:beta回归算法 编辑:程序博客网 时间:2024/05/16 13:10
Automatic properties
A real pain in the neck for all programmers writing object oriented code has always been declaring public properties for all the private fields. This is a lot of tedious work, especially because almost all properties will be a simple get and set mapping to the private field, without anything clever added, like this:
private string name;public string Name{ get { return name; } set { name = value; }}
With a simple property like that, we could pretty much just as well have declared the field as public and used it directly, instead of adding the extra layer of a property. However, the guidelines of OOP tells us to do it this way, and most of us resists the temptation of doing it the easy way. With C# 3.0 we don't have to deal with this dilemma anymore! The above example can now be written like this instead:
public string Name{ get; set;}
Or using even less space, like this:
No field declaration, and no code to get and set the value of the field. All of that is handled automatically by the compiler, which will automatically create a private field and populate the get and set method with the basic code required to read and write the field. From the outside, this will look just like a regular property, but you saved a lot of extra keystrokes and your class will be less bloated. Of course, you can still use the old way, as shown in our first example - this is simply an extra feature that you can use, if you feel like it.public string Name { get; set; }
- Automatic Properties in C#
- C#3.0 automatic properties
- C# 3.0 的自动属性(Automatic Properties)
- C# 3.0 的自动属性(Automatic Properties)
- C# 3.0 的自动属性(Automatic Properties)
- C# 3.0 的自动属性(Automatic Properties)
- C# 3.0 的自动属性(Automatic Properties)
- C# 3.0 的自动属性(Automatic Properties)
- 新的C#语言特性:自动属性(Automatic Properties)
- C#3.0新特性系列:自动属性(Automatic Properties)
- Properties and Indexers in C#
- VS2008&C#3.5语言特色系列[视频]:自动属性(Automatic Properties)
- Automatic variables in Makefile
- Automatic variables in GNU Make
- C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- Setting up Automatic NDK Builds in Eclipse
- Setting up Automatic NDK Builds in Eclipse
- Setting up Automatic NDK Builds in Eclipse
- C++ 代理类
- Viusal Studio 2010——C#中的数组
- 信息系统项目管理师学习札记7
- 驱动监控进程的创建
- 配置电信网通双线双IP
- Automatic Properties in C#
- Linq to sql用法解析---join
- EXCEL VBA常用到的技巧
- ant非法字符:\65279 错误
- UPnP协议编程实践
- Colection
- Eclipse TPTP 分析程序性能
- c++长方体与三角形类
- open函数中O_CLOEXEC标志的开关