如何在不返回值的情况下使用三元运算符? [英] How Do I Can Using Ternary Operator Without Return A Value?
问题描述
大家好,
我想像这样使用三元运算符,但是它不起作用.
Hello people,
I want use the ternary operator like this, but it doesn''t work.
var itemToRemove = myCollection.Any(p => p != null).FirstOrDefault();
itemToRemove != null ?? myCollection.Remove(itemToRemove); //this don't compile!
It''s Possible?
推荐答案
这里有两个问题:
1)Any
返回一个bool
,而不是一个集合,因此您不能在其上使用FirstOrDefault
.
2)null-coalescing运算符不能那样工作.阅读MSDN描述:? OPerator [^ ]
我不太确定您认为该代码应达到的目标,所以我真的无法提出任何解决方案-也许如果您告诉我们您正在尝试做的事情可能会有所帮助?
There are a couple of problems here:
1)Any
returns abool
, not a collection, so you can''t useFirstOrDefault
on it.
2) The null-coalescing operator does not work like that. Read the MSDN description: ?? OPerator[^]
I''m not quite sure what the heck you think that code should achieve, so I can''t really suggest anything to fix it - perhaps if you told us what you were trying to do it might help?
进一步了解OriginalGriff的解决方案,
三元运算符也不能那样工作
http://www.dotnetperls.com/ternary [
遵循MCY的建议,并使用if
Further to OriginalGriff''s solution,
The Ternary Operator doesn''t work like that either
http://www.dotnetperls.com/ternary[^] - the clue is in the name "operator"
Follow the advice from MCY and useif
if( itemToRemove != null)
myCollection.Remove(itemToRemove);
您的代码未使用三元运算符:您正在使用"??"计算一个参数(变量)的运算符,如果该值非空,则返回所计算的值;如果该值为空,则在"??"之后的值返回.示例:
Your code does not make use of the ternary operator: you are using the "??" operator which evaluates an argument (a variable), and if the value is non-null, returns the evaluated value; if the value is null, then the value after the "??" is returned. Example:
string s1 = null;
string t1 = s1 ?? "whoop de doo"; // t1 now contains "whoop de doo"
string s2 = "hoop la";
string t2 = s2 ?? "yabba dabba doo" // t2 now contains "hoop la"
您可以使用三元运算符,Microsoft将该三元运算符称为条件运算符",该运算符必须始终 返回值:
You can use the ternary operator, referred to by Microsoft as the "conditional operator," which must always return a value:
string t3 = (s1 != null) ? t2 : "shake, rattle, and roll";
// t3 contains "shake, rattle, and roll"
,两者都包含???和三元运算符,您只能在每个段"中使用一个语句,并且必须返回某些内容.
显然,您的目标是从集合中删除一个项目.请记住,.NET Collections Remove< T>操作员返回一个布尔值,指示移除成功或失败;您可以使用该返回值,也可以忽略它.如果需要跟踪删除是否成功,请尝试以下操作:
With both ?? and ternary operators you are limited to one statement in each "segment," and you must return something.
Clearly, your goal here is to remove an item from a collection. Keep in mind that .NET Collections Remove<T> operator returns a boolean value indicating the success or failure of a removal; you can use that return value, or ignore it. If you need to keep track of whether the removal was successful, try this:
bool itemIsRemoved = myCollection.Remove(myCollection.FirstOrDefault(p => p != null));
这是一个挑战:
List<string> lStr = new List<string> {null, string.Empty, "0", "1", "41", "2", "3", "4", "5" };
var itmIsRemoved1 = lStr.Remove(lStr.FirstOrDefault(str => str != null));
var itmIsRemoved2 = lStr.Remove(lStr.FirstOrDefault(str => str == "4"));
var itmIsRemoved3 = lStr.Remove(lStr.FirstOrDefault(str => str == string.Empty));
var itmIsRemoved4 = lStr.Remove(lStr.FirstOrDefault(str => str == null));
您认为执行上述代码的每一行之后,'itmIsRemoved的值以及字符串列表中的值是什么?
What do you think the value of ''itmIsRemoved, and the values in the List of strings, are after each line of the code above is executed ?
这篇关于如何在不返回值的情况下使用三元运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!