c# - c#中的if返回语句

标签 c# if-statement return overriding

我正在重写一个虚拟方法,我需要根据 bool 表达式返回一个值。

  public override float Q()
    {
        return (float)Math.Sqrt(2 * A* B / C);
    }

如果上面显示的计算值小于 D,我需要为 Q() 返回一个本地成员(称为 D)。我尝试使用常规 if 语句,但它不会返回值,除非它在 ​​if 语句之外返回。任何帮助,将不胜感激。

最佳答案

跳过 if 怎么样?共:

return Math.Max(D, Math.Sqrt (2*A*B/C));

或使用三元运算符
return  Math.Sqrt (2*A*B/C) < D ? D :  Math.Sqrt (2*A*B/C); // too verbose but you get the idea

关于c# - c#中的if返回语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15979122/

相关文章:

JQuery .Ajax 返回数据作为对象而不是 HTML

c# - 域模型中的域服务与实体方法

C# RabbitMQ 设计方法

iphone - 在 Objective C 中检查 NSString 的 NOT <null> 值的条件

javascript - else 语句不执行 JavaScript

angular - Angular 中的 else 语句

c++ - [[noreturn]] 函数如何具有返回类型?

c# - 安全等于 : Why is inequality still consistently less time?

c# - 如何将多个列表与自定义序列组合

javascript - 我的函数返回一个字符串,但比较总是错误的