简体   繁体   English

if语句和“?/:”运算符之间的区别

[英]Difference between the if statement and “?/:” operator

I have this piece of code here. 我这里有这段代码。 In case ServiceType.Register: I have what appears to be two equivalent statements one using a regular if statement and one using the ternary operator ?/: . case ServiceType.Register:我有两个等效语句,一个使用常规if语句,另一个使用三元运算符?/: :。 For the if statement VS reports no error. 对于if语句,VS报告没有错误。 However with this line: 但是这条线:

IsXML == true ? PopulateRegister(ParseType.Xml) : PopulateRegister(ParseType.Str);

VS is erroring out saying: VS错误地说:

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement 只有赋值,调用,递增,递减,等待和新对象表达式才能用作语句

Anyone know why regular if statement has no error but if you use "?/:" (one liner) throws an error? 任何人都知道为什么常规if语句没有错误,但如果你使用“?/:”(一个班轮)抛出错误? Pic is also attached. Pic也附上了。

switch (this.ServiceType)
{
    case SerivceType.Login:
        PopulateLogin();
        break;
    case SerivceType.Register:

        if (IsXML == true)
            PopulateRegister(ParseType.Xml);
        else
            PopulateRegister(ParseType.Str);

        IsXML == true ? PopulateRegister(ParseType.Xml) : PopulateRegister(ParseType.Str);

        break;
    case SerivceType.Verify:
        PopulateVerify();
        break;
}

在此输入图像描述

The ?: operator is used for conditional assignments, not operations. ?:运算符用于条件赋值,而不是操作。

The statement: 该声明:

IsXML == true ? PopulateRegister(ParseType.Xml) : PopulateRegister(ParseType.Str);

would be valid if the method PopulateRegister returns a value, and if you assigned that value to something. 如果PopulateRegister方法返回一个值,并且您将该值赋给某个​​值,则该方法有效。 For example, this would be valid: 例如,这将是有效的:

string result = (someCondition) ? "condition is true" : "condition is false";

You might want to use the conditional this way instead (note that we are using the ParseType enum value as a return type of the condition, and it is acting as the parameter to your method): 您可能希望以这种方式使用条件(请注意,我们使用ParseType枚举值作为条件的返回类型,并且它充当您的方法的参数):

PopulateRegister((IsXML) ? ParseType.Xml : ParseType.Str);

Note that the above is possible, but can produce hard to understand/debug/maintain code and would generally not be thought of as a best practice. 请注意,上述内容是可行的,但可能会产生难以理解/调试/维护的代码,并且通常不会被视为最佳实践。

A conditional operator is a shortcut to assign a value to a variable. 条件运算符是为变量赋值的快捷方式。 But your method PopulateRegister does not return a value( void ). 但是您的方法PopulateRegister不返回值( void )。 So use an if-else instead. 所以请使用if-else代替。

The reason for the error is that if..then..else is a statement, meaning it is supposed NOT to return a value, think of it as if it was a void function of sorts (not really so, but i hope you get the idea) while the ? : 错误的原因是if..then..else是一个语句,意味着它不应该返回一个值,把它想象成是一个类型的void函数(不是真的如此,但我希望你得到这个想法)而? : ? : operator is an expression, meaning it returns a value and since the functions you are using are void there is nothing to return. ? : operator是一个表达式,意味着它返回一个值,因为你使用的函数是void所以没有任何东西可以返回。 Hope this helps 希望这可以帮助

IsXML == true ? IsXML == true? PopulateRegister(ParseType.Xml) : PopulateRegister(ParseType.Str); PopulateRegister(ParseType.Xml):PopulateRegister(ParseType.Str);

You are not assigning the result to anything. 您没有将结果分配给任何内容。

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. 条件运算符(?:)根据布尔表达式的值返回两个值中的一个。 Following is the syntax for the conditional operator. 以下是条件运算符的语法。 [Emphasis mine] [强调我的]

https://msdn.microsoft.com/en-us/library/ty67wk28.aspx https://msdn.microsoft.com/en-us/library/ty67wk28.aspx

There is nothing to return as your functions are of return type void. 没有什么可以返回的,因为你的函数是返回类型void。

if is a statement and ?: is a ternary operator , to be used within an expression . if是一个语句,?:是一个三元运算符 ,用于表达式中 Its operands must be expressions yielding a value. 它的操作数必须是产生值的表达式。 Your method PopulateRegister doesn't return a value, so it won't work. 您的方法PopulateRegister不返回值,因此它不起作用。

You cannot provide an arbitrary expression in place of a statement, so what you thought would be a shortcut won't work. 您不能提供任意表达式来代替语句,因此您认为快捷方式不起作用。 The following is not a valid statement: 以下不是有效的声明:

// this is an expression, not a statement
IsXML == true ? PopulateRegister(ParseType.Xml) : PopulateRegister(ParseType.Str);

This would be valid: 这是有效的:

// this is a valid statement
PopulateRegister(IsXML ? ParseType.Xml : ParseType.Str);

Side notes: 附注:

  • Don't use IsXML == trueIsXML is a boolean expression itself, so an equality comparison to true is completely redundant and just clutters your code. 不要使用IsXML == true - IsXML本身就是一个布尔表达式,因此与true的相等比较完全是多余的,只会使代码IsXML
  • The error you are observing is compile-time. 您正在观察的错误是编译时。 Hence it is not thrown by the code itself, it's reported by the compiler. 因此,它不是由代码本身引发的,它是由编译器报告的。

Think of the block: (b) ? (c) : (d) 想一想: (b) ? (c) : (d) (b) ? (c) : (d) as a one stand-alone object value . (b) ? (c) : (d)作为一个独立的对象 Such that b is boolean. 这样b就是布尔值。 c and d are inherited from object . cd继承自object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM