Effective C# 学习笔记(一) 用属性替代公有变量
2011-07-02 23:07
375 查看
好久没写东西了,现在找到本好书,写下读书笔记,在帮助自己记忆的同时,与大家分享。 :)
意义: 可以统一管理公有变量的set 和 get行为
作用:
1. 对对象属性添加多线程支持
public class Customer
{
private object syncHandle = new object();
private string name;
public string Name
{
get
{
lock (syncHandle)
return name;
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException(
"Name cannot be blank",
"Name");
lock (syncHandle)
name = value;
}
}
// More Elided.
}
将属性设置为 虚属性
public class Customer
{
public virtual string Nam
{
get;
set;
}
}
在接口中定义属性
public interface INameValuePair<T>
{
string Name
{
get;
}
T Value
{
get;
set;
}
}
可以分别定义属性的get set 的可访问性
public class Customer
{
public virtual string Name
{
get;
protected set;
}
// remaining implementation omitte
}
调用索引器数值参数索引可以用来做data binding
public int this[int index]
{
get { return theValues[index]; }
set { theValues[index] = value; }
}
// Accessing an indexer:
int val = someObject[i];
非数值类型参数可以用作maps 和 Dictionaries
public Address this[string name]
{
get { return adressValues[name]; }
set { adressValues[name] = value; }
}
多维索引
public int this[int x, int y]
{
get { return ComputeValue(x, y); }
}
public int this[int x, string name]
{
get { return ComputeValue(x, name);
}
属性与变量比较
属性的改变只会影响到类定义的部分,而公有变量重定义为属性后会影响到所有使用该变量的地方
属性访问器不应有过多影响性能操作,如访问数据库,保证其在使用时和使用变量有类似的性能
意义: 可以统一管理公有变量的set 和 get行为
作用:
1. 对对象属性添加多线程支持
public class Customer
{
private object syncHandle = new object();
private string name;
public string Name
{
get
{
lock (syncHandle)
return name;
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException(
"Name cannot be blank",
"Name");
lock (syncHandle)
name = value;
}
}
// More Elided.
}
将属性设置为 虚属性
public class Customer
{
public virtual string Nam
{
get;
set;
}
}
在接口中定义属性
public interface INameValuePair<T>
{
string Name
{
get;
}
T Value
{
get;
set;
}
}
可以分别定义属性的get set 的可访问性
public class Customer
{
public virtual string Name
{
get;
protected set;
}
// remaining implementation omitte
}
调用索引器数值参数索引可以用来做data binding
public int this[int index]
{
get { return theValues[index]; }
set { theValues[index] = value; }
}
// Accessing an indexer:
int val = someObject[i];
非数值类型参数可以用作maps 和 Dictionaries
public Address this[string name]
{
get { return adressValues[name]; }
set { adressValues[name] = value; }
}
多维索引
public int this[int x, int y]
{
get { return ComputeValue(x, y); }
}
public int this[int x, string name]
{
get { return ComputeValue(x, name);
}
属性与变量比较
属性的改变只会影响到类定义的部分,而公有变量重定义为属性后会影响到所有使用该变量的地方
属性访问器不应有过多影响性能操作,如访问数据库,保证其在使用时和使用变量有类似的性能
相关文章推荐
- Effective C# 学习笔记(四)使用Conditional Attributes 替代 #if
- objective-C学习笔记(三)数据成员:属性与实例变量
- 学习笔记之--属性、字段、变量
- AppleScript学习笔记1——变量和属性
- IPhone开发学习笔记:属性变量如何release?
- OC学习笔记-属性与成员变量的区别
- C语言学习笔记4——变量属性
- Effective C# 学习笔记(十二) 多用成员变量初始化,少用指定赋值
- 学习笔记之--属性、字段、变量
- 『iOS学习笔记』 - 变量 属性 方法 实现
- 【黑马程序员-学习笔记】OC-成员变量与属性
- iOS学习笔记56(Runtime)-Objective-C Runtime 运行时之二:成员变量与属性
- Objective-C学习笔记(十七)——成员变量和属性详解
- JMeter学习笔记--JMeter属性和变量
- object-c学习笔记:属性变量(property)
- object-c学习笔记:属性变量(property)
- COCOS学习笔记--滑动条Slider及其控件属性
- React学习笔记-8-属性和状态详解
- QT学习笔记—.pro变量解析
- 类别,属性,成员变量,arc学习总结