Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do nothing keyword in C#? [closed]

Tags:

c#

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","")
like image 269
Sperick Avatar asked Oct 18 '12 14:10

Sperick


People also ask

How do you say else do nothing in C?

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.

What is a do nothing function?

A function that does nothing (yet) is an indicator that something should be done, but hasn't been implemented yet.

How do you code do nothing?

Answer: use _ = 0 .

What is null loop in C?

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) { /* ...


2 Answers

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", "");
like image 171
BoltClock Avatar answered Sep 18 '22 11:09

BoltClock


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", ""))();
like image 30
Ani Avatar answered Sep 18 '22 11:09

Ani