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

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

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

c#winforms

提问by Sunny Mark

I am getting this error in condiotional operator.

我在条件运​​算符中收到此错误。

string remarks="";
AddDgvNew[6, i].Value==null?remarks="":remarks=AddDgvNew[6,i].Value.ToString();

采纳答案by C.Evenhuis

Use

string remarks = AddDgvNew[6, i].Value==null?"":AddDgvNew[6,i].Value.ToString();

回答by Jon Skeet

Yes - because you're not doinganything with the result of the conditional expression. You've got a conditional expression which is trying to be a whole statement. In a simpler version:

是的 - 因为你没有对条件表达式的结果任何事情。你有一个条件表达式,它试图成为一个完整的语句。在更简单的版本中:

bool condition = true;
int x = 10;
int y = 5;

// This is invalid
condition ? x : y;

What did you want to dowith the result of the conditional expression? If the point was to assign it to a variable, then you need to do that. Currently you have twoseparate statements: one declares remarksand assigns it a value; the second is justthe conditional expression.

你想对条件表达式的结果什么?如果重点是将它分配给一个变量,那么您需要这样做。目前你有两个独立的语句:一个声明remarks并赋值;第二个只是条件表达式。

If you're trying to do something else, you'll need to clarify what you're looking for.

如果您正在尝试做其他事情,则需要澄清您在寻找什么。