简单的语法问题 [英] Easy Syntax Question

查看:72
本文介绍了简单的语法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

return arr.Length == 0? null:arr [item] .ToString();


谁能告诉我这个语法是什么意思?


它是否基本上意味着如果arr .Length等于0然后返回null,否则

返回数组中的项目??

return arr.Length == 0 ? null : arr[item].ToString();

Can anyone tell me what this syntax means?

Does it basically mean if arr.Length is equal to 0 then return null, else
return the item in the array ??

推荐答案

是的。它相当于:


if(arr.Length == 0)

{

返回null;

}

else

{

return arr [item] .ToString();

}


BTW,我讨厌三元运营商。我讨厌C语言,我讨厌C ++,

现在我也厌恶它在C#中。在C#中只有一个地方你在没有它的情况下绝对不能没有它,那就是在链接构造函数的时候

来电,你在哪里不允许插入if声明:


MyClass(s​​tring foo):base(foo == null:""?foo)


否则,它'这只是一个回归到看看我能用多少代码
可以填充到一行的日子。丑陋且难以维护,恕我直言。

Yes. It is equivalent to:

if (arr.Length == 0)
{
return null;
}
else
{
return arr[item].ToString();
}

BTW, I hate the "ternary" operator. I hated it in C, I hated it in C++,
and now I hate it in C#, too. There''s only one place in C# where you
absolutely can''t do without it, and that''s when chaining constructor
calls, where you''re not allowed to insert "if" statements:

MyClass(string foo) : base(foo == null : "" ? foo)

Otherwise, it''s just a throwback to the days of "look how much code I
can stuff onto one line". Ugly and difficult to maintain, IMHO.


<" Terry McNamee" < N / A>>写道:
<"Terry McNamee" <n/a>> wrote:
return arr.Length == 0? null:arr [item] .ToString();

任何人都可以告诉我这个语法是什么意思吗?

它是否基本上意味着如果arr.Length等于0然后返回null,否则
返回数组中的项目??
return arr.Length == 0 ? null : arr[item].ToString();

Can anyone tell me what this syntax means?

Does it basically mean if arr.Length is equal to 0 then return null, else
return the item in the array ??




完全正确。这是一个条件运算符。基本上,条件

(最左边的操作数)被计算,如果它的计算结果为true,则计算

中间操作数并且是表达式的结果。

否则,最右边的操作数被评估,并且是

表达式的结果。


-

Jon Skeet - < sk *** @ pobox.com>
http:/ /www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Exactly. It''s a conditional operator. Basically, the condition
(leftmost operand) is evaluated, and if it evaluates to true, the
middle operand is evaluated and is the result of the expression.
Otherwise, the rightmost operand is evaluated and is the result of the
expression.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Terry,


在大多数情况下,是的。但是,为了正确,它应该是:


return((arr == null || arr.Length == 0)?null:arr [item] .ToString()) ;


这假设arr可能为null。


希望这会有所帮助。


- -

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam.guard.caspershouse.com


" Terry McNamee" < N / A>在消息中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
Terry,

For the most part, yes. However, to be correct, it should be:

return ((arr == null || arr.Length == 0) ? null : arr[item].ToString());

This assumes that arr might be null.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Terry McNamee" <n/a> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
return arr .Length == 0? null:arr [item] .ToString();

任何人都可以告诉我这个语法是什么意思吗?

它是否基本上意味着如果arr.Length等于0然后返回null,否则
返回数组中的项目??
return arr.Length == 0 ? null : arr[item].ToString();

Can anyone tell me what this syntax means?

Does it basically mean if arr.Length is equal to 0 then return null, else
return the item in the array ??



这篇关于简单的语法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆