C#属性:如何使用自定义设置属性没有私人领域? [英] C# properties: how to use custom set property without private field?
本文介绍了C#属性:如何使用自定义设置属性没有私人领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想这样做:
公开姓名
{
搞定;
组
{
DoSomething的();
? =价值
}
}
是否有可能使用生成的自动私人领域?
结果或者是要求我实现这种方式:
私人字符串名称;
公共字符串名称
{
得到
{
返回名称;
}
组
{
DoSomething的();
名称=值
}
}
解决方案
在你想无论是在getter或无法使用自动属性了二传手的定制做任何事情。
I want to do this:
public Name
{
get;
set
{
dosomething();
??? = value
}
}
Is it possible to use the auto-generated private field?
Or is it required that I implement it this way:
private string name;
public string Name
{
get
{
return name;
}
set
{
dosomething();
name = value
}
}
解决方案
Once you want to do anything custom in either the getter or the setter you cannot use auto properties anymore.
这篇关于C#属性:如何使用自定义设置属性没有私人领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文