Bri*_*cky 1 c# overriding if-statement return
我重写了一个虚方法,我需要根据bool表达式返回一个值.
public override float Q()
{
return (float)Math.Sqrt(2 * A* B / C);
}
Run Code Online (Sandbox Code Playgroud)
如果上面显示的计算值小于D,我需要为Q()返回一个本地成员(称之为D).我尝试使用常规if语句,但除非在if语句之外返回,否则它不会返回值.任何帮助,将不胜感激.
如何if完全跳过:
return Math.Max(D, Math.Sqrt (2*A*B/C));
Run Code Online (Sandbox Code Playgroud)
或与三元运算符
return Math.Sqrt (2*A*B/C) < D ? D : Math.Sqrt (2*A*B/C); // too verbose but you get the idea
Run Code Online (Sandbox Code Playgroud)