c# - 表达式 `?`中的符号 `maxY = (maxY > pt.Y) ? maxY : pt.Y;`是什么意思

标签 c#

符号?在下面的代码中是什么意思:

  for(int i = 0 ; i < Hpts.BranchCount;i++)
    { foreach(Point3d pr in Hpts.Branches[i]){
        minX = (minX > pt.X) ? pt.X : minX;
        minY = (minY > pt.Y) ? pt.Y : minX;
        maxX = (maxX > pt.X) ? maxX : pt.X;
        maxY = (maxY > pt.Y) ? maxY : pt.Y;
      }
    }

最佳答案

它被称为条件运算符。参见documentation

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

condition ? first_expression : second_expression;

The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result. Only one of the two expressions is evaluated.

以你的情况为例:

minX = (minX > pt.X) ? pt.X : minX;
  • minX > pt.X条件
  • pt.Xfirst_expression
  • minXsecond_expression

这意味着如果条件为trueminX 的值将等于pt.X,否则它不会改变,因为第二个表达式本身就是变量。

关于c# - 表达式 `?`中的符号 `maxY = (maxY > pt.Y) ? maxY : pt.Y;`是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21868501/

相关文章:

c# - ormlite - 生成 poco 文件

c# - 在 Xamarin Forms 的点击/点击事件处理程序中传递对象

c# - TcpClient 超时与 NetworkStream 超时

c# - 如何防止 SQL Azure 中的 ASP.NET SQL 注入(inject)

c# - HttpSelfHostServer 和 HttpContext.Current

c# - 分层 TreeView 实现

c# - 从 IIS 运行 PsExec : PsExecSVC hanging on remote host

c# - 在 .NET Web 应用程序中填充缓存时的线程管理

c# - WPF 无法聚焦 TextBox

c# - Unity 3d Server脚本C#中的错误