ternary-operator

Is there a difference between a ternary operator and an if statement in C#? [duplicate]

孤者浪人 提交于 2019-12-06 06:28:57
问题 This question already has answers here : Nullable types and the ternary operator: why is `? 10 : null` forbidden? [duplicate] (9 answers) Conditional operator cannot cast implicitly? (3 answers) Closed 6 years ago . I'm working with a nullable DateTime object and ran into some strange behavior. Here's a sample function: public DateTime? Weird() { DateTime check = DateTime.Now; DateTime? dt; if (check == DateTime.MinValue) dt = null; else dt = Viewer.ActiveThroughUTC.ToLocalTime(); //this line

Initialization of structure with ternary operator

若如初见. 提交于 2019-12-06 06:20:05
问题 Why the ternary operator cannot be used to initialize a structure type, while it can be used to initialize a base type like int ? Example code : #include <stdio.h> #define ODD 1 int main(int argc, const char *argv[]) { static struct pair_str { int first; int second; } pair = ( ODD ) ? {1,3} : {2,4}; // ERROR printf("pair %d %d\n", pair.first, pair.second); int number = (ODD) ? 1 :2; // FINE return 0; } Compiler errors : /home/giuseppe/struct.c: In function ‘main’: /home/giuseppe/struct.c:12

Java Ternary Operator inside ternary operator, how evaluated?

北城以北 提交于 2019-12-06 06:18:23
问题 Very basic question I suppose, I just wanted to know how this code is read: return someboolean ? new someinstanceofsomething() : someotherboolean ? new otherinstance() : new third instance(); I guess now as I'm writing it I kind of understand the statement. It returns option one if true but then does another boolean check if false and returns one of the two remaining options? I'm going to continue to leave this question because I have not seen it before and maybe others have not as well.

Ternary/null coalescing operator and assignment expression on the right-hand side?

被刻印的时光 ゝ 提交于 2019-12-06 05:24:52
问题 While experimenting with ternary and null coalesce operators in C# I discovered that it is possible to use assignments on the right-hand side of expressions, for example this is a valid C# code: int? a = null; int? b = null; int? c = a ?? (b = 12); int? d = a == 12 ? a : (b = 15); Strangely enough, not only the assignment on the right-hand side of the expression is evaluated to its own right-hand side (meaning that the third line here is evaluated to 12 and not to something like b = 12 =>

Checking if undefined and null in jQuery

拥有回忆 提交于 2019-12-06 05:23:25
问题 I'm a bit confused when checking if null or undefined and if I should be using !== or != and "undefined" or undefined. Here is some code I'm working on. Where am I going wrong with my null/unudefined etc? var c = (jQuery(this).prop("target") != null && jQuery(this).prop("target") != undefined && jQuery(this).prop("target").toLowerCase() == "_blank") ? 1 : 0; Thanks 回答1: Both null and undefined are "falsy" values, thus they can be checked like they were boolean values. Thus, there's no sense

Multiple css rules with ternary operator

馋奶兔 提交于 2019-12-06 02:51:34
Within a .hover() event, I have the following code: $(this).css('background-position', circle.includesXY(e.pageX, e.pageY) ? 'bottom' : ''); Could somebody explain how I can add more property:value pairs to .css() without compromising the ternary operator? If not, how should it be rewritten? Explosion Pills .css also accepts an object: $(this).css(circle.includesXY(e.pageX, e.pageY) ? { property1: 'value1', property2: 'value2' } : { property1: 'value3', property2: 'value4' } ); 来源: https://stackoverflow.com/questions/12228372/multiple-css-rules-with-ternary-operator

How can I closely achieve ?: from C++/C# in Python?

给你一囗甜甜゛ 提交于 2019-12-05 19:50:31
问题 In C# I could easily write the following: string stringValue = string.IsNullOrEmpty( otherString ) ? defaultString : otherString; Is there a quick way of doing the same thing in Python or am I stuck with an 'if' statement? 回答1: In Python 2.5, there is A if C else B which behaves a lot like ?: in C. However, it's frowned upon for two reasons: readability, and the fact that there's usually a simpler way to approach the problem. For instance, in your case: stringValue = otherString or

Is there difference between ternary operator and if condition?

 ̄綄美尐妖づ 提交于 2019-12-05 18:14:37
Is there difference between ternary operator and if condition in php ? if yes, kindly provide. The ternary operator is an operator , so it forms an expression. So it will have a value that you can assign to a variable or use however you want. It is used for simple situations where a variable can take two possible values depending on a condition. For example: $status = $age > 18 ? 'adult' : 'child'; Although possible, you should not nest the ternary operator . The control structure if is something absolutely different. The only thing they have in common that both evaluate a condition (true

Casting pointers and the ternary ?: operator. Have I reinvented the wheel?

旧街凉风 提交于 2019-12-05 17:35:26
问题 The last line of this code fails to compile with castingAndTernary.cpp:15: error: conditional expression between distinct pointer types ‘D1*’ and ‘D2*’ lacks a cast A really smart compiler could have no difficulty because both can be safely casted to B* (the base class). I'm reluctant to use static_cast and dynamic_cast and so on - I'm worried that I'll mix up the classes someday and get undefined behaviour. That's why I created the up_cast template. This template does the bare minimum in

Unexpected output when using a ternary operator and final variable

江枫思渺然 提交于 2019-12-05 16:49:05
问题 Consider this code snippet: public static void main(String[] args) { int z1 = 0; final int z2 = 0; System.out.println(false ? z1 : 'X'); System.out.println(false ? z2 : 'X'); } When running this code, I would expect to see two X in your console. However, the real output is: 88 X If we take a look at the Java specifications regarding the ternary operator, we found that If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type