在C#中创建Begin Break和End [英] Creating Begin break and end in C#

查看:42
本文介绍了在C#中创建Begin Break和End的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在C#中做到这一点吗?

#define BEGIN_BLOCK do
#define EXIT_BLOCK break;
#define END_BLOCK while(false);

Does anyone know how to do the equivelant of this in C#?

#define BEGIN_BLOCK do
#define EXIT_BLOCK break;
#define END_BLOCK while(false);

推荐答案

即使C#支持此类内容,以这种方式使用编译器指令也只能使代码变得模糊并使其成为标准.新手很难加入团队.

错误的做法,错误的技术,以及简单的错误主意.
Even if C# supported this kind of stuff, using compiler directives in such a way serves only to obfuscate the code and make it harder for new guys joining the team to come up to speed.

Bad practice, bad technique, and just plain bad idea.


这里是执行以上操作的一种C#方式:

Here''s a more C#-ish way of doing what you did above:

static class ExecuteMultiple
{
    public static bool Execute(params Func<bool>[] functions)
    {
        foreach (var function in functions)
        {
            if (function())
            {
                return false;
            }
        }

        return true;
    }
}

bool SomeFuncionNew()
{
    bool bRetVal = false;

    bRetVal = ExecuteMultiple.Execute
        (
            new Func<bool>[]
            {
                () => badResult1 = Foo1(),
                () => badResult2 = Foo2()
            }
        );

    if (!bRetVal)
    {
        //Do some other clean up work.
    }

    return bRetVal;
}


您为什么要?如果您对此进行了说明,那么也许人们可以为您提供更好的解决方案.
Why would you want to? If you explain that, then perhaps people may be able to give you better solutions.


这篇关于在C#中创建Begin Break和End的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆