c#并非所有代码路径都返回值错误

Gab*_*abe 1 .net c# stringbuilder function

我在c#中遇到以下错误:此代码"并非所有代码路径都返回值"我正在尝试使用它创建编程语言.任何帮助是极大的赞赏.

private Expr ParseExpr()
{
    if (this.index == this.tokens.Count)
    {
        throw new System.Exception("expected expression, got EOF");
    }
    if (this.tokens[this.index] is Text.StringBuilder)
    {
        string Value = ((Text.StringBuilder)this.tokens[this.index++]).ToString();
        StringLiteral StringLiteral = new StringLiteral();
        StringLiteral.Value = Value;
    }
    else if (this.tokens[this.index] is int)
    {
        int intvalue = (int)this.tokens[this.index++];
        IntLiteral intliteral = new IntLiteral();
        intliteral.Value = intvalue;
        return intliteral;    
    }
    else if (this.tokens[this.index] is string)
    {
        string Ident = (string)this.tokens[this.index++];
        Variable var = new Variable();
        var.Ident = Ident;
        return var;
    }
    else
    {
        throw new System.Exception("expected string literal, int literal, or variable");
    }
}                     
Run Code Online (Sandbox Code Playgroud)

And*_*rei 9

你忘了在那里回报价值:

 if (this.tokens[this.index] is Text.StringBuilder)
    {
        string Value = ((Text.StringBuilder)this.tokens[this.index++]).ToString();
        StringLiteral StringLiteral = new StringLiteral();
        StringLiteral.Value = Value;
        //return Anything
    }
Run Code Online (Sandbox Code Playgroud)

您还应该在函数结束时返回值.


And*_*rei 5

如果出现以下情况,您忘记在第二个中返回任

if (this.tokens[this.index] is Text.StringBuilder)
{
    string Value = ((Text.StringBuilder)this.tokens[this.index++]).ToString();
    StringLiteral StringLiteral = new StringLiteral();
    StringLiteral.Value = Value;
    return StringLiteral;
}
Run Code Online (Sandbox Code Playgroud)