如何在C#中退出“if块”?

huangapple go评论55阅读模式
英文:

How can i exit 'if block' in c#

问题

我想要在 C# 上退出 if 块
当条件为真时
我该怎么做?

这是代码示例

string str = "Hello World";

void func(bool isCheckA, bool isCheckB)
{
    if (isCheckA)
    {
        if (isCheckB)
        {
            // 退出 if 块
        }

        str = "Bye World";
    }

    Console.WriteLine(str);
}

如果 isCheckA 为真且 isCheckB 为真
str 不会被改变
A 和 B 的布尔值必须分别检查

英文:

I want Exit if block on C#
when condition is true
how can i do that?

Here is Code Example

string str = "Hello World"

void func(bool isCheckA, bool isCheckB)
{
    if(isCheckA)
    {
        if(isCheckB)
        {
            // Exit if Block
        }

        str = "Bye World"
    }
    
    Console.WriteLine(str);
}

if isCheckA is true and isCheckB is true
str is not changed

the bool value A and B must be checked separately by if

答案1

得分: 2

(1) 使用 else

void func(bool isCheckA, bool isCheckB)
{
    if (isCheckA)
    {
        if (isCheckB)
        { }
        else
            str = "再见,世界";
    }
    Console.WriteLine(str);
}

(2) 使用 !

void func(bool isCheckA, bool isCheckB)
{
    if (isCheckA)
    {
        if (!isCheckB)
        {
            str = "再见,世界";
        }
    }
    Console.WriteLine(str);
}
英文:

These seem like your clear choices to me:

(1) use else

void func(bool isCheckA, bool isCheckB)
{
	if (isCheckA)
	{
		if (isCheckB)
		{ }
		else
			str = "Bye World";
	}
	Console.WriteLine(str);
}

(2) use !

void func(bool isCheckA, bool isCheckB)
{
	if (isCheckA)
	{
		if (!isCheckB)
		{
			str = "Bye World";
		}
	}
	Console.WriteLine(str);
}

答案2

得分: 1

我建议尝试早期返回策略。这种策略可以防止出现嵌套多层的“IF”语句。使您的代码易于阅读。当然,这取决于要求,但如果有可能,我会使用它。

void func(bool isCheckA, bool isCheckB)
{
    if (!isCheckA)
    {
        Console.WriteLine("第一个条件失败。正在退出");
        return;
    }
    
    if (isCheckB)
    {
        Console.WriteLine("第二个条件为真。正在退出");
        return;
    }
    
    Console.WriteLine("再见,世界");
}
英文:

I'll suggest to try early return strategy. This strategy prevents from having nested multiple level 'IF'. Makes your code easy to read. Of course it depends on requirements, but if there is possibility I'm using it.

void func(bool isCheckA, bool isCheckB)
{
	if(!isCheckA)
	{
		Console.Writeline("First condition failed. Exiting");
		return;
	}
	
	if(isCheckB)
	{
		Console.WriteLine("Second condition is true. Exiting");
		return;
	}
	
	Console.WriteLine("Bye World");
}

答案3

得分: 0

以下是您要翻译的内容:

C# 允许像这样进行本地跳转[1]

```csharp
void func( bool isCheckA, bool isCheckB )
{
    if( isCheckA )
    {
        if( isCheckB )
        {
            goto afterIf; // https://xkcd.com/292/
        }

        str = "Bye World"
    }
    
afterIf:
    Console.WriteLine( str );
}

或者您可以使用 break,前提是您有一个虚拟的外部循环(不会运行其主体超过一次):

void func( bool isCheckA, bool isCheckB )
{
    do
    {
        if( isCheckA )
        {
            if( isCheckB )
            {
                break;
            }

            str = "Bye World"
        }
    }
    while( false );

    Console.WriteLine( str );
}

或者使用本地方法:

void func( bool isCheckA, bool isCheckB )
{
    void innerFunc()
    {
        if( isCheckA )
        {
            if( isCheckB )
            {
                return;
            }

            str = "Bye World"
        }
    }
    innerFunc();

    Console.WriteLine( str );
}

虽然我相信每个人都会同意,您应该使用以下方式:

void func( bool isCheckA, bool isCheckB )
{
    if( isCheckA && !isCheckB )
    {
        str = "Bye World"
    }
    
    Console.WriteLine( str );
}

<details>
<summary>英文:</summary>

[C# allows the use of `goto` for local jumps][1] like this:

```csharp
void func( bool isCheckA, bool isCheckB )
{
    if( isCheckA )
    {
        if( isCheckB )
        {
            goto afterIf; // https://xkcd.com/292/
        }

        str = &quot;Bye World&quot;
    }
    
afterIf:
    Console.WriteLine( str );
}

Or you can use break provided you have a dummy outer loop (which won't run its body more than once):

void func( bool isCheckA, bool isCheckB )
{
    do
    {
        if( isCheckA )
        {
            if( isCheckB )
            {
                break;
            }

            str = &quot;Bye World&quot;
        }
    }
    while( false );

    Console.WriteLine( str );
}

Or a local method:

void func( bool isCheckA, bool isCheckB )
{
    void innerFunc()
    {
        if( isCheckA )
        {
            if( isCheckB )
            {
                return;
            }

            str = &quot;Bye World&quot;
        }
    }
    innerFunc();

    Console.WriteLine( str );
}

Though I'm sure everyone will agree you should do just this instead:

void func( bool isCheckA, bool isCheckB )
{
    if( isCheckA &amp;&amp; !isCheckB )
    {
        str = &quot;Bye World&quot;
    }
    
    Console.WriteLine( str );
}

huangapple
  • 本文由 发表于 2023年6月26日 11:55:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定