类似于 Pass in Python for C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/581343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:08:25  来源:igfitidea点击:

Similar to Pass in Python for C#

c#python

提问by

In python we can ...

在python中,我们可以...

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

Is there a similar way to do this in C#?

在 C# 中是否有类似的方法可以做到这一点?

回答by Lo?c Wolff

Use empty braces.

使用空大括号。

int a = 5;
if (a == 5) {}
else {
  Console.Write("Hello World");
}

回答by vartec

Empty block:

空块:

{}

回答by Ali

Why not just say:

为什么不直接说:

if (a != 5) 
{
   Console.Write("Hello World");
}

回答by Jon Skeet

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");
}

回答by Christian Witts

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");
}

回答by Kent Boogaart

Is passused in the context of a loop? If so, use the continuestatement:

是否pass在循环上下文中使用?如果是这样,请使用以下continue语句:

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

    Console.WriteLine("Hello World");
}

回答by Umer

In case you don't want to use empty block, use

如果您不想使用空块,请使用

;

so the code should look like

所以代码应该看起来像

int a = 5;
if (a == 5)
  ;
else 
{
  Console.Write("Hello World");
}

although, code readability still suffer.

尽管如此,代码可读性仍然受到影响。