布尔值将在构造函数中保存为true或false。存储条件的最简单方法是变量将在将来更改并重新计算?
本质上,我想创建一个新类,并询问它“您刚才写的条件语句是对还是错?”
public class MyClass
{
public bool Condition { get; }
public MyClass(bool condition)
{
Condition = condition;
}
}
public class MyOtherClass
{
int foo = 1;
int goo = 2;
MyClass myClass;
public MyOtherClass()
{
myClass = new MyClass(foo > goo);
}
public void SomeMethod()
{
foo = 3;
if (myClass.Condition)
//Do Something
}
}
答案 0 :(得分:4)
您将需要使用谓词,例如,使用内置的Func
泛型类型:
public class MyClass
{
public MyClass(Func<bool> predicate)
{
this.Predicate = predicate;
}
public Func<bool> Predicate { get; }
}
var bah = new MyClass(() => foo > baz)
if (bah.Predicate())
{
}