conditional-operator

How to print the null values if the dictionary has it using LINQ

假如想象 提交于 2020-07-22 05:58:04
问题 I have dictionary with the following keys and values. I am trying to print the dictionary, but nothing prints where there is a null value. How do I print "null" in the output? Dictionary<string, object> dic1 = new Dictionary<string, object>(); dic1.Add("id", 1); dic1.Add("name", "john"); dic1.Add("grade", null); Console.WriteLine(string.Join(Environment.NewLine, dic1.Select(a => $"{a.Key}: {a.Value}"))); Here is the output I get: id: 1 name: john grade: 回答1: You can use the null-coalescing

Why is a ternary operator with two constants faster than one with a variable?

南楼画角 提交于 2020-07-17 06:35:51
问题 In Java, I have two different statements which accomplish the same result through using ternary operators, which are as follows: num < 0 ? 0 : num; num * (num < 0 ? 0 : 1); It appears that the second statement is unnecessarily complex and would take longer than the first, however when I recorded the time that each took, using the following code, the results were as follows: final long startTime = System.currentTimeMillis(); Random rand = new Random(); float[] results = new float[100000000];

Why is a ternary operator with two constants faster than one with a variable?

邮差的信 提交于 2020-07-17 06:35:19
问题 In Java, I have two different statements which accomplish the same result through using ternary operators, which are as follows: num < 0 ? 0 : num; num * (num < 0 ? 0 : 1); It appears that the second statement is unnecessarily complex and would take longer than the first, however when I recorded the time that each took, using the following code, the results were as follows: final long startTime = System.currentTimeMillis(); Random rand = new Random(); float[] results = new float[100000000];

Weird results for conditional operator with GCC and bool pointers

删除回忆录丶 提交于 2020-06-27 06:42:11
问题 In the following code, I memset() a stdbool.h bool variable to value 123 . (Perhaps this is undefined behaviour?) Then I pass a pointer to this variable to a victim function, which tries to protect against unexpected values using a conditional operation. However, GCC for some reason seems to remove the conditional operation altogether. #include <stdio.h> #include <stdbool.h> #include <string.h> void victim(bool* foo) { int bar = *foo ? 1 : 0; printf("%d\n", bar); } int main() { bool x; bool

Optimize ternary operator

£可爱£侵袭症+ 提交于 2020-06-24 05:32:20
问题 I came across this code written by someone else. Is this usage of the conditional operator recommended or commonly used? I feel it is less maintainable - or is it just me? Is there any alternate way of writing this? exp_rsp_status = req.security_violation ? (dis_prot_viol_rsp && is_mstr) ? uvc_pkg::MRSP_OKAY : uvc_pkg::MRSP_PROTVIOL : req.slv_req.size() ? ((is_mst_abort_rsp && dis_mst_abort_rsp) || ((req.slv_req[0].get_rsp_status()==uvc_pkg::MRSP_PROTVIOL) && dis_prot_viol_rsp) || (is_mst