Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何消除;否则,如果;声明_C#_.net_Asp.net - Fatal编程技术网

C# 如何消除;否则,如果;声明

C# 如何消除;否则,如果;声明,c#,.net,asp.net,C#,.net,Asp.net,我的函数以字符串的形式从某个网页获取QueryString。 我需要把它包起来,检查一下,我必须使用什么策略 现在我的代码看起来很难看(我想是的): 一个查询字符串中不能同时有两个字段名 如何将此代码更改为mo可读 使用开关案例。使用开关案例。代码可读,您可以在常见条件下使用一些嵌套,以使零件更清晰 您不能使用Switch,因为比较必须是静态值。代码是可读的,您可以在常见条件下使用一些嵌套,以使零件更清晰 您不能使用Switch,因为比较必须是静态值。在检查现有字段时,我至少会删除一些重复项:

我的函数以
字符串的形式从某个网页获取QueryString
。 我需要把它包起来,检查一下,我必须使用什么策略

现在我的代码看起来很难看(我想是的):

一个查询字符串中不能同时有两个字段名


如何将此代码更改为mo可读

使用开关案例。

使用开关案例。

代码可读,您可以在常见条件下使用一些嵌套,以使零件更清晰


您不能使用Switch,因为比较必须是静态值。

代码是可读的,您可以在常见条件下使用一些嵌套,以使零件更清晰


您不能使用Switch,因为比较必须是静态值。

在检查现有字段时,我至少会删除一些重复项:

public QueryStringParser(string QueryString) {
    if (string.IsNullOrEmpty(QueryString))
        this._mode = Mode.First;
    else {
        bool has_1st = QueryString.Contains(_FristFieldName);
        bool has_2nd = QueryString.Contains(_SecondFieldName);
        if      ( has_1st && !has_2nd) this._mode = Mode.Second;
        else if (!has_1st &&  has_2nd) this._mode = Mode.Third;
        else
            throw new ArgumentException("QueryString has wrong format");
    }
}

在检查现有字段时,我至少要消除一些重复:

public QueryStringParser(string QueryString) {
    if (string.IsNullOrEmpty(QueryString))
        this._mode = Mode.First;
    else {
        bool has_1st = QueryString.Contains(_FristFieldName);
        bool has_2nd = QueryString.Contains(_SecondFieldName);
        if      ( has_1st && !has_2nd) this._mode = Mode.Second;
        else if (!has_1st &&  has_2nd) this._mode = Mode.Third;
        else
            throw new ArgumentException("QueryString has wrong format");
    }
}
注释和缩进
它已经很好看了

public QueryStringParser(string QueryString)
        {
            // Input is NULL STRING
            if (string.IsNullOrEmpty(QueryString))
            {
                this._mode = Mode.First;
            }

            // Query using FirstName
            else if (QueryString.Contains(_FristFieldName) &&
                    !QueryString.Contains(_SecondFieldName))
            {
                this._mode = Mode.Second;
            }

            //Query using SecondName
            else if (!QueryString.Contains(_FristFieldName) &&
                      QueryString.Contains(_SecondFieldName))
            {
                this._mode = Mode.Third;
            }

            //Insufficient info to Query data
            else
            {
                throw new ArgumentException("QueryString has wrong format");
            }
        }
我希望她现在在你看来很漂亮

祝你好运

注释和缩进
它已经很好看了

public QueryStringParser(string QueryString)
        {
            // Input is NULL STRING
            if (string.IsNullOrEmpty(QueryString))
            {
                this._mode = Mode.First;
            }

            // Query using FirstName
            else if (QueryString.Contains(_FristFieldName) &&
                    !QueryString.Contains(_SecondFieldName))
            {
                this._mode = Mode.Second;
            }

            //Query using SecondName
            else if (!QueryString.Contains(_FristFieldName) &&
                      QueryString.Contains(_SecondFieldName))
            {
                this._mode = Mode.Third;
            }

            //Insufficient info to Query data
            else
            {
                throw new ArgumentException("QueryString has wrong format");
            }
        }
我希望她现在在你看来很漂亮


祝你好运

代码是可读的。您可以通过删除括号将其缩短,因为这些语句只是一行:

public QueryStringParser(string QueryString)
        {
            //check if string is empty
            if (string.IsNullOrEmpty(QueryString))
                this._mode = Mode.First;
            else
            {
               bool hasFirst = QueryString.Contains(_FristFieldName);
               bool hasSecond= QueryString.Contains(_SecondFieldName);

               //check if string contains first field name but not second field name
               if (hasFirst  && !hasSecond)
                   this._mode = Mode.Second;
               //check if string contains second field name but not first field name
               else if (!hasFirst && hasSecond)
                   this._mode = Mode.Third;
               //default - error
               else
                   throw new ArgumentException("QueryString has wrong format");
            }
        }
还-删除了对Contains方法的重复调用

我不建议使用任何速记操作符,因为这会使代码的可读性大大降低


最后-在线评论

代码是可读的。您可以通过删除括号将其缩短,因为这些语句只是一行:

public QueryStringParser(string QueryString)
        {
            //check if string is empty
            if (string.IsNullOrEmpty(QueryString))
                this._mode = Mode.First;
            else
            {
               bool hasFirst = QueryString.Contains(_FristFieldName);
               bool hasSecond= QueryString.Contains(_SecondFieldName);

               //check if string contains first field name but not second field name
               if (hasFirst  && !hasSecond)
                   this._mode = Mode.Second;
               //check if string contains second field name but not first field name
               else if (!hasFirst && hasSecond)
                   this._mode = Mode.Third;
               //default - error
               else
                   throw new ArgumentException("QueryString has wrong format");
            }
        }
还-删除了对Contains方法的重复调用

我不建议使用任何速记操作符,因为这会使代码的可读性大大降低


最后-在线评论

您可以将模式的确定放入单独的方法中,并返回模式值;这样就可以消除if-else语句

public QueryStringParser(string QueryString)
{
    this._mode = DetermineMode(QueryString);
}

private Mode DetermineMode(string QueryString)
{
    // Input is NULL STRING
    if (string.IsNullOrEmpty(QueryString))
        return Mode.First;

    bool hasFirst = QueryString.Contains(_FristFieldName);
    bool hasSecond = !QueryString.Contains(_SecondFieldName);

    // Query using FirstName
    if (hasFirst && !hasSecond)
        return Mode.Second;

    //Query using SecondName
    if (!hasFirst && hasSecond)
        return Mode.Third;

    //Insufficient info to Query data
    throw new ArgumentException("QueryString has wrong format");
}

[编辑]删除了对字段名称存在性的双重检查,改为使用变量。

您可以将模式的确定放在单独的方法中,并返回模式值;这样就可以消除if-else语句

public QueryStringParser(string QueryString)
{
    this._mode = DetermineMode(QueryString);
}

private Mode DetermineMode(string QueryString)
{
    // Input is NULL STRING
    if (string.IsNullOrEmpty(QueryString))
        return Mode.First;

    bool hasFirst = QueryString.Contains(_FristFieldName);
    bool hasSecond = !QueryString.Contains(_SecondFieldName);

    // Query using FirstName
    if (hasFirst && !hasSecond)
        return Mode.Second;

    //Query using SecondName
    if (!hasFirst && hasSecond)
        return Mode.Third;

    //Insufficient info to Query data
    throw new ArgumentException("QueryString has wrong format");
}

[编辑]删除了对字段名存在性的双重检查,改为使用变量。

使用诸如ReSharper或Refactor Pro之类的工具!帮助整理代码并鼓励良好实践。

使用诸如ReSharper或Refactor Pro之类的工具!帮助整理您的代码并鼓励良好实践。

此处的切换可能并不优雅。由于没有在所有IFs中计算单个表达式…您不能在此处使用开关开关开关中的情况必须是固定的/静态开关在此处可能不优雅。由于没有在所有IFs中对单个表达式求值…您不能在此处使用开关开关开关中的情况必须是固定/静态的。这可能是一个示例,但您添加的注释并没有使任何内容更清楚,代码是清晰的。这是因为我实际上不知道代码的上下文;i、 e.它的用途。我放置的注释只是为了给Stremlenye一个代码外观的概念。最后,我希望Stremlenye会将适当的注释放在那里……这在我看来比mkj的方法更易于维护。如果某些内容发生了变化,那么修改它就容易一点。这可能是一个示例,但您添加的注释并没有使任何内容变得更清晰,代码是清晰的。这是因为我实际上不知道代码的上下文;i、 e.它的用途。我放置的注释只是为了给Stremlenye一个代码外观的概念。最后,我希望Stremlenye会将适当的注释放在那里……这在我看来比mkj的方法更易于维护。如果有什么变化,修改this.Oops会稍微容易一点。在看到您的评论之前编写了完全相同的代码:)糟糕。在看到您的评论之前编写了完全相同的代码:)