conditional-operator

Why does the ternary operator unexpectedly cast integers?

自古美人都是妖i 提交于 2019-11-26 02:09:57
问题 I have seen it discussed somewhere that the following code results in obj being a Double , but that it prints 200.0 from the left hand side. Object obj = true ? new Integer(200) : new Double(0.0); System.out.println(obj); Result: 200.0 However, if you put a different object in the right hand side, e.g. BigDecimal , the type of obj is Integer as it should be. Object obj = true ? new Integer(200) : new BigDecimal(0.0); System.out.println(obj); Result: 200 I presume that the reason for this is

Conditional operator assignment with Nullable<value> types?

不想你离开。 提交于 2019-11-26 01:29:41
问题 EmployeeNumber = string.IsNullOrEmpty(employeeNumberTextBox.Text) ? null : Convert.ToInt32(employeeNumberTextBox.Text), I often find myself wanting to do things like this ( EmployeeNumber is a Nullable<int> as it\'s a property on a LINQ-to-SQL dbml object where the column allows NULL values). Unfortunately, the compiler feels that \"There is no implicit conversion between \'null\' and \'int\'\", even though both types would be valid in an assignment operation to a nullable int on their own.

How do I use the conditional operator (? :) in Ruby?

主宰稳场 提交于 2019-11-26 01:25:41
问题 How is the conditional operator ( ? : ) used in Ruby? For example, is this correct? <% question = question.size > 20 ? question.question.slice(0, 20)+\"...\" : question.question %> 回答1: It is the ternary operator, and it works like in C (the parenthesis are not required). It's an expression that works like: if_this_is_a_true_value ? then_the_result_is_this : else_it_is_this However, in Ruby, if is also an expression so: if a then b else c end === a ? b : c , except for precedence issues. Both

Return type of &#39;?:&#39; (ternary conditional operator)

爷,独闯天下 提交于 2019-11-26 00:58:33
问题 Why does the first return a reference? int x = 1; int y = 2; (x > y ? x : y) = 100; While the second does not? int x = 1; long y = 2; (x > y ? x : y) = 100; Actually, the second did not compile at all - \"not lvalue left of assignment\". 回答1: Expressions don't have return types, they have a type and - as it's known in the latest C++ standard - a value category. A conditional expression can be an lvalue or an rvalue . This is its value category. (This is somewhat of a simplification, in C++11

How do I use the conditional operator?

寵の児 提交于 2019-11-26 00:34:01
问题 I\'ve always wondered how to write the \"A ? B : C\" syntax in a C++ compatible language. I think it works something like: (Pseudo code) If A > B C = A Else C = B Will any veteran C++ programmer please help me out? 回答1: It works like this: (condition) ? true-clause : false-clause It's most commonly used in assignment operations, although it has other uses as well. The ternary operator ? is a way of shortening an if-else clause, and is also called an immediate-if statement in other languages (

Returning null as an int permitted with ternary operator but not if statement

亡梦爱人 提交于 2019-11-26 00:25:15
问题 Let\'s look at the simple Java code in the following snippet: public class Main { private int temp() { return true ? null : 0; // No compiler error - the compiler allows a return value of null // in a method signature that returns an int. } private int same() { if (true) { return null; // The same is not possible with if, // and causes a compile-time error - incompatible types. } else { return 0; } } public static void main(String[] args) { Main m = new Main(); System.out.println(m.temp());

How do I use the ternary operator ( ? : ) in PHP as a shorthand for “if / else”?

送分小仙女□ 提交于 2019-11-26 00:17:55
问题 Based on the examples from this page, I have the working and non-working code samples below. Working code using if statement: if (!empty($address[\'street2\'])) echo $address[\'street2\'].\'<br />\'; Non-working code using ternary operator: $test = (empty($address[\'street2\'])) ? \'Yes <br />\' : \'No <br />\'; // Also tested this (empty($address[\'street2\'])) ? \'Yes <br />\' : \'No <br />\'; UPDATE After Brian\'s tip, I found that echoing $test outputs the expected result. The following

Is there a conditional ternary operator in VB.NET?

吃可爱长大的小学妹 提交于 2019-11-26 00:15:30
问题 In Perl (and other languages) a conditional ternary operator can be expressed like this: my $foo = $bar == $buz ? $cat : $dog; Is there a similar operator in VB.NET? 回答1: Depends upon the version. The If operator in VB.NET 2008 is a ternary operator (as well as a null coalescence operator). This was just introduced, prior to 2008 this was not available. Here's some more info: Visual Basic If announcement Example: Dim foo as String = If(bar = buz, cat, dog) [EDIT] Prior to 2008 it was "IIf",

To ternary or not to ternary? [closed]

☆樱花仙子☆ 提交于 2019-11-25 23:53:48
问题 I\'m personally an advocate of the ternary operator: () ? : ; I do realize that it has its place, but I have come across many programmers that are completely against ever using it, and some that use it too often. What are your feelings on it? What interesting code have you seen using it? 回答1: Use it for simple expressions only : int a = (b > 10) ? c : d; Don't chain or nest ternary operators as it hard to read and confusing: int a = b > 10 ? c < 20 ? 50 : 80 : e == 2 ? 4 : 8; Moreover, when

Benefits of using the conditional ?: (ternary) operator

烈酒焚心 提交于 2019-11-25 23:15:01
问题 What are the benefits and drawbacks of the ?: operator as opposed to the standard if-else statement. The obvious ones being: Conditional ?: Operator Shorter and more concise when dealing with direct value comparisons and assignments Doesn\'t seem to be as flexible as the if/else construct Standard If/Else Can be applied to more situations (such as function calls) Often are unnecessarily long Readability seems to vary for each depending on the statement. For a little while after first being