C# 只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17820277/
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-10 10:34:16  来源:igfitidea点击:

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

c#c#-4.0

提问by Robert

I have this foreach section and i am trying to add a line after my "result = string.Format" but i get the following error "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement" can someone tell me what i am doing wrong.

我有这个 foreach 部分,我试图在“result = string.Format”之后添加一行,但出现以下错误“只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句“谁能告诉我我做错了什么。

foreach (DataRow record in table.Rows)
{
    string key = record["Code"] + "_" + record["Description"];
    int row = (int)rownum[key];

    string date = formatDate(record["ApptDate"].ToString(), "_");

    string result = string.Empty;
    if (record["Dosage"].ToString() != string.Empty)
        result = string.Format("{0}/{1}", test.SharedLib.Application.RemoveTrailingZeroes(record["Dosage"].ToString()), 
                                          test.SharedLib.Application.RemoveTrailingZeroes(record["Units"].ToString()));
    if (record["Dosage"].ToString() != string.Empty)
        result.StartsWith("/") && result.EndsWith("/") ? result.Replace("/", string.Empty) : result;
    else if (record["Units"].ToString() != string.Empty)
        result = record["Units"].ToString();

    dataTable.Rows[row]["ApptDate" + date] = result;
}

回答by D.R.

You have to do something with the result of your expression (e.g. assign it to result?!)

您必须对表达式的结果做一些事情(例如将其分配给结果?!)

For more information you should really format and tell us more about your code...SO users are not your personal compiler/debugger ;-)

有关更多信息,您应该真正格式化并告诉我们更多有关您的代码的信息......所以用户不是您的个人编译器/调试器;-)

回答by cdhowie

if (record["Dosage"].ToString() != string.Empty)
    result.StartsWith("/") && result.EndsWith("/") ? result.Replace("/", string.Empty) : result;

The second line there is not a statement, it is an expression. Not all expressions can be statements in C#, so this is a syntax error.

第二行没有语句,它是一个表达式。并非所有表达式都可以是 C# 中的语句,因此这是一个语法错误。

Presumably you meant to assign the result of this to result:

据推测,您打算将此结果分配给result

if (record["Dosage"].ToString() != string.Empty)
    result = (result.StartsWith("/") && result.EndsWith("/")) ? result.Replace("/", string.Empty) : result;

Further, you should consider enclosing the bodies of if/ elseblocks with braces ({}). Without braces, the way in which these blocks nest is not intuitive, and it will hamper future maintenance. (For example, can you tell which ifblock the else ifblock will belong to? Will the next person who inherits this project be able to not only tell the difference, but understand what nesting was intended? Make it explicit!)

此外,您应该考虑用大括号 ( )将if/else块的主体括起来{}。如果没有大括号,这些块的嵌套方式不直观,并且会妨碍未来的维护。(例如,你能说出这个if区块else if属于哪个区块吗?下一个继承这个项目的人不仅能说出区别,还能理解嵌套的意图吗?说清楚!)

回答by Tyler

result.StartsWith("/") && result.EndsWith("/") ? result.Replace("/", string.Empty) : result;

This line does nothing with the result of the ternary operator. I assume you'd want to assign it to result

这一行对三元运算符的结果没有任何作用。我假设你想把它分配给结果