14

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","")
6
  • 23
    What's wrong with if?
    – Mat
    Oct 18, 2012 at 14:32
  • 2
    In your example, you should use an "if" statement instead of a ternary operator.
    – Fabio
    Oct 18, 2012 at 14:33
  • 4
    ternary operation should have result.
    – John Woo
    Oct 18, 2012 at 14:33
  • 2
    I cant help but feel like im getting trolled on this question. =p Oct 18, 2012 at 17:40
  • 2
    This question was closed as "not a real question". It may not be a great question, yet it seems to be a real question since it was asked again: stackoverflow.com/questions/41146905/…
    – Tot Zam
    Dec 15, 2016 at 4:38

10 Answers 10

44

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", "");
0
22

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", ""))();
1
  • 15
    Credit for actually providing a working solution. Even if it is friggin retarded... Oct 19, 2012 at 6:36
7

Setting aside the fact that this is a bad idea, you actually can create a 'DoNothing' method using an attribute to conditionally emit the method call (note this relies on the convention that you don't #define a 'NEVER' condition.) Because of the ConditionalAttribute, calls to the method will not be emitted and you essentially have a 'nop' section of code.

       [Conditional("NEVER")] 
       public void DoNothing() 
       { 
       } 

Still, as several others have already pointed out, this is not recommended.

5

Why do you want to use a ternary operator, if you only want to do something in one of the cases ? Why don't you just invert the condition and use an if statement ?

if (!officeDict.ContainsKey("0"))
{
    officeDict.Add("0","")
}
1
  • 1
    Good advice: Don't write spaces before question marks, it looks unprofessional. Oct 26, 2012 at 16:45
3

The ternary operator is for expressions, not statements.

You want an if statement.

3

Why not just use an if statement so the code looks like this:

if(!officeDict.ContainsKey("0"))
    officeDict.Add("0","")
2

Why not write it with a simple if?

if (!officeDict.ContainsKey("0"))
    officeDict.Add("0","")

It will be far more readable.

2

Why don't you just use an IF statement for this?

if(!officeDict.ContainsKey("0")) officeDict.Add("0","");
2

Use an if statement instead.

if (!officeDict.ContainsKey("0")) 
{
    officeDict.Add("0","")
}
-3

Try this (grinns)

    if (officeDict.ContainsKey("0")) 
       Thread.Sleep(1) 
   else
       officeDict.Add("0","")

Not the answer you're looking for? Browse other questions tagged or ask your own question.