conditional-operator

Why is this code invalid in C#?

别来无恙 提交于 2019-12-17 06:06:35
问题 The following code will not compile: string foo = "bar"; Object o = foo == null ? DBNull.Value : foo; I get: Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string' To fix this, I must do something like this: string foo = "bar"; Object o = foo == null ? DBNull.Value : (Object)foo; This cast seems pointless as this is certainly legal: string foo = "bar"; Object o = foo == null ? "gork" : foo; It seems to me that

One-line list comprehension: if-else variants

谁说我不能喝 提交于 2019-12-17 05:21:32
问题 It's more about python list comprehension syntax. I've got a list comprehension that produces list of odd numbers of a given range: [x for x in range(1, 10) if x % 2] This makes a filter - I've got a source list, where I remove even numbers ( if x % 2 ). I'd like to use something like if-then-else here. Following code fails: >>> [x for x in range(1, 10) if x % 2 else x * 100] File "<stdin>", line 1 [x for x in range(1, 10) if x % 2 else x * 100] ^ SyntaxError: invalid syntax There is a python

Java conditional operator ?: result type

我与影子孤独终老i 提交于 2019-12-17 05:01:22
问题 I'm a bit puzzled about the conditional operator. Consider the following two lines: Float f1 = false? 1.0f: null; Float f2 = false? 1.0f: false? 1.0f: null; Why does f1 become null and the second statement throws a NullPointerException? Langspec-3.0 para 15.25 sais: Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The

Method Call using Ternary Operator

自闭症网瘾萝莉.ら 提交于 2019-12-17 03:17:31
问题 While playing around with new concepts, I came across the Ternary Operator and it's beauty. After playing with it for a while, I decided to test its limits. However, my fun was ended quickly when I couldn't get a certain line of code to compile. int a = 5; int b = 10; a == b ? doThis() : doThat() private void doThis() { MessageBox.Show("Did this"); } private void doThat() { MessageBox.Show("Did that"); } This line gives me two errors: Error 1 Only assignment, call, increment, decrement, and

Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)

試著忘記壹切 提交于 2019-12-17 03:03:42
问题 So for binary operators on booleans, Java has & , | , ^ , && and || . Let's summarize what they do briefly here: JLS 15.22.2 Boolean Logical Operators &, ^, and | JLS 15.23 Conditional-And Operator && JLS 15.24 Conditional-Or Operator || For & , the result value is true if both operand values are true ; otherwise, the result is false . For | , the result value is false if both operand values are false ; otherwise, the result is true . For ^ , the result value is true if the operand values are

Ternary operator ?: vs if…else

≯℡__Kan透↙ 提交于 2019-12-17 02:44:10
问题 In C++, is the ?: operator faster than if()...else statements? Are there any differences between them in compiled code? 回答1: Depends on your compiler, but on any modern compiler there is generally no difference. It's something you shouldn't worry about. Concentrate on the maintainability of your code. 回答2: It is not faster. There is one difference when you can initialize a constant variable depending on some expression: const int x = (a<b) ? b : a; You can't do the same with if-else . 回答3: I

Error: lvalue required in this simple C code? (Ternary with assignment?)

萝らか妹 提交于 2019-12-17 02:38:24
问题 I have : #include<stdio.h> int main() { int a=5,b=6; (a>b)?b=a:b=b; // Here is the error return 0; } But if I replace : (a>b)?b=a:b=b; // Error with (a>b)?(b=a):(b=b); // No-Error I understand the lvalue is a value to which something can be assigned and how is it different from rvalue , but why is the extra parenthesis making the difference. 回答1: Assignment has a lower precedence than the ternary operator so the line evaluates like: ((a>b)?b=a:b)=b; use: b=(a>b)?a:b; 回答2: Actually, in C, this

Error: lvalue required in this simple C code? (Ternary with assignment?)

蹲街弑〆低调 提交于 2019-12-17 02:38:08
问题 I have : #include<stdio.h> int main() { int a=5,b=6; (a>b)?b=a:b=b; // Here is the error return 0; } But if I replace : (a>b)?b=a:b=b; // Error with (a>b)?(b=a):(b=b); // No-Error I understand the lvalue is a value to which something can be assigned and how is it different from rvalue , but why is the extra parenthesis making the difference. 回答1: Assignment has a lower precedence than the ternary operator so the line evaluates like: ((a>b)?b=a:b)=b; use: b=(a>b)?a:b; 回答2: Actually, in C, this

Nullable type issue with ?: Conditional Operator

廉价感情. 提交于 2019-12-17 01:41:12
问题 Could someone explain why this works in C#.NET 2.0: Nullable<DateTime> foo; if (true) foo = null; else foo = new DateTime(0); ...but this doesn't: Nullable<DateTime> foo; foo = true ? null : new DateTime(0); The latter form gives me an compile error "Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'." Not that I can't use the former, but the second style is more consistent with the rest of my code. 回答1: This

What are the PHP operators “?” and “:” called and what do they do?

有些话、适合烂在心里 提交于 2019-12-16 19:43:51
问题 What are the ? and : operators in PHP? For example: (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) 回答1: This is the conditional operator . $x ? $y : $z means "if $x is true, then use $y ; otherwise use $z ". It also has a short form. $x ?: $z means "if $x is true, then use $x ; otherwise use $z ". People will tell you that ?: is "the ternary operator". This is wrong. ?: is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary