Is there a keyword to 'do nothing' in C#?
For example I want to use a ternary operator where one of the actions is to take no action:
officeDict.ContainsKey("0") ? DO NOTHING : officeDict.Add("0","")
If you want your last else to do nothing, you can use the continue keyword inside it. For any conditional you can define the conditional to do nothing by defining an empty code block {} for it or by simply ommit the case.
A function that does nothing (yet) is an indicator that something should be done, but hasn't been implemented yet.
Answer: use _ = 0 .
a null statement is used to supply an empty loop body to the iteration statement. 6 EXAMPLE 3 A null statement may also be used to carry a label just before the closing } of a compound statement. while (loop1) { /* ... */ while (loop2) { /* ...
No, there is no such keyword. Besides, the ?:
operator is not meant for deciding actions. It's meant for deciding values (or, more technically, value expressions).
You really, really want to use an if condition for this sort of decision-making instead:
if (!officeDict.ContainsKey("0"))
officeDict.Add("0", "");
As others have pointed out, using an if
statement is far more idiomatic here.
But to answer what you ask, you can use lambdas and delegates:
// Bad code, do not use:
(officeDict.ContainsKey("0") ? (Action)(() => { }) : () => officeDict.Add("0", ""))();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With