【发布时间】:2019-10-01 12:00:47
【问题描述】:
我最近正在阅读 Github 上的一些代码,我遇到了以下行,
if (((tmp = rx.IndexOf("<")) >= 0) && (rx.IndexOf(">") > tmp ))
特别是
(tmp = rx.IndexOf("<") >= 0)
并且在 if 语句的下一部分的同一行中立即使用 tmp 变量进行比较
(rx.IndexOf(">") > tmp )
其中一个变量由一个字符串 indexOf() 方法设置,然后“赋值语句本身”正在使用一个大于或等于相等运算符进行评估。
起初我认为这是一个错字,但在通过一个简单的控制台应用程序评估代码时,我发现它是有效的,而且是一个很好的捷径。
问题是“这个的技术术语是什么?”因为我在各种 C# 帮助站点中找不到任何解释。
一个演示如何使用语句的示例控制台应用程序。
public static void Main()
{
// first test - the actual code I found in gitHub
int tmp;
int tmp2;
string rx = " < test>";
// the below line is the subject of the question.
if (((tmp = rx.IndexOf("<")) >= 0) && (rx.IndexOf(">") > tmp )){
Console.WriteLine("The Close brace is after the opening brace!");
}
// additional test
int r;
Console.WriteLine(r = 25 + 3);
Console.WriteLine(r);
// and another
int w = -1;
Console.WriteLine(" The index of '<' is greater than 0 : " + _
((w = rx.IndexOf("<")) > 0).ToString() + _
" and the value of w is " + w.ToString());
}
上述代码的输出如下。 再次,我理解代码的工作原理,我想知道这在技术上叫什么?
The Close brace is after the opening brace!
28
28
The index of '<' is greater than 0 : True and the value of w is 2
【问题讨论】:
-
我不知道这个术语,但这个功能是从
c“继承”的。 -
没有专门的术语,只是所有花括号语言中的正常语法。赋值也是一个表达式。您只是看不到它经常被使用,因为代码变得非常密集且难以阅读。