可能重复:
C# - if语句中的赋值
我发现自己做了以下多次
if (e.row.FindControl("myLiteral") is Literal)
{
(e.row.FindControl("myLiteral") as Literal).Text = "textData";
}
Run Code Online (Sandbox Code Playgroud)
有没有办法替换"if"部分并简化setter:
(e.row.FindControl("myLiteral") <some operator that combines is and as> .text = "textData";
Run Code Online (Sandbox Code Playgroud)
编辑:我之前应该提到这一点 - 我想完全删除'if'.
"some operator"应该在内部执行此操作,并且仅当e.row.FindControl是文字时才设置".text"
通常我不会将它们合并一样,开始用的-我想无论是使用强制或者我会用as和空校验:
Literal literal = e.row.FindControl("myLiteral") as Literal;
if (literal != null)
{
literal.Text = "textData";
}
Run Code Online (Sandbox Code Playgroud)