看源代码刚学到的,当属性的set块中还有其它调用或较复杂的操作时,不要重复设置对象的属性。道理很简单啊,还是贴段代码吧

,代码演示的是怎样做是否重复的判断而不是重现上面描述的情景.
class EntryPoint
{
[STAThread]
static void Main(string[] args)
{
Test test = new Test();
test.Name = "yicone";
test.Name = "yicone";
test.Name = "yic";
Console.ReadLine();
}
}
public class Test
{
int i = 1;
public string name;
public string Name
{
get{return name;}
set
{
if(value == Name)
{
Console.WriteLine("第{0}次set name属性失败!", i++);
Console.WriteLine("原因:与当前Test类的实例的Name属性值相同,不需要更改");
return;
}
name = value;
Console.WriteLine("第{0}次set name属性成功", i++);
}
}
}
p.s. 调整了措词,以避免不必要的误会.
yicone
-The future is worth fighting for.
posted on 2005-09-11 00:38
yicone 阅读(774)
评论(5) 编辑 收藏 所属分类:
C# Programming