我的问题是,如果可能的话,做这样的事情:
public Class Test
{
public int Number { get; set; }
private string text;
public string Text
{
if (Number > 5)
{
set {text = value;}
get {return text;}
}
}
}
最佳答案
不,但你可以这样做:
public class Test {
public int Number { get; set; }
private string _Text;
public string Text {
get {
if(Number > 5) {
return _Text;
} else {
//DEFAULT value here.
return null;
}
}
set {
if(Number > 5) {
_Text = value;
} else {
//DEFAULT Value.
_Text = null;
}
}
}
}
如果您使用的是 Visual Studio,我还会查看预处理器指令。根据您尝试使用代码的方式,这些可能更有用。
预处理指令: https://msdn.microsoft.com/en-us/library/3sxhs2ty.aspx
关于c# - 如何使用 if 语句创建属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40595148/