tags:

views:

193

answers:

6

In python we can ..

a = 5
if a == 5:
          pass #Do Nothing
else:
      print "Hello World"

I wonder if it a similar way to do this in C#

+9  A: 

Use empty braces.

int a = 5;
if (a == 5) {}
else {
  Console.Write("Hello World");
}
Loïc Wolff
+2  A: 

Empty block:

{}
vartec
+9  A: 

Why not just say:

if (a != 5) 
{
   Console.Write("Hello World");
}
Ali
+3  A: 

Either use an empty block as suggested in other answers, or reverse the condition:

if (a != 5)
{
    Console.WriteLine("Hello world");
}

or more mechanically:

if (!(a == 5))
{
    Console.WriteLine("Hello world");
}
Jon Skeet
+1  A: 

A better question would be why you would want to do such a thing. If you're not planning on doing anything then leave it out, rather.

int a = 5;
if (a != 5) {
    Console.Write("Hello World");
}
Christian Witts
+3  A: 

Is pass used in the context of a loop? If so, use the continue statement:

for (var i = 0; i < 10; ++i)
{
    if (i == 5)
    {
        continue;
    }

    Console.WriteLine("Hello World");
}

HTH, Kent

Kent Boogaart