如何用if语句制作财产 [英] How to make a property with a if-statement
本文介绍了如何用if语句制作财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是,如果可能的话,要做这样的事情:
my question is, if it's possible, to do something like this:
public Class Test
{
public int Number { get; set; }
private string text;
public string Text
{
if (Number > 5)
{
set {text = value;}
get {return text;}
}
}
}
推荐答案
否,但是您可以执行以下操作:
No, but you could do something like:
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,我还将检出预处理器指令.根据您尝试使用代码的方式,这些方法可能会更有用.
I would also check out Preprocessor Directives if you are using Visual Studio. These might be more helpful depending on how you are trying to use the code.
预处理指令: https://msdn.microsoft.com/en-us/library/3sxhs2ty.aspx
这篇关于如何用if语句制作财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文