如何使用内联c#if而不是长c#if语句

如何使用内联c#if而不是长c#if语句,c#,if-statement,C#,If Statement,如何使用单行if语句来减少此类编码的代码行数 if (string.IsNullOrEmpty(txtpictext.Text)) { Cmd.Parameters.AddWithValue("@pictext", DBNull.Value); } else { Cmd.Parameters.AddWithValue("@pictext, txtpictext.Text

如何使用单行if语句来减少此类编码的代码行数

        if (string.IsNullOrEmpty(txtpictext.Text))
        {
            Cmd.Parameters.AddWithValue("@pictext", DBNull.Value);
        }
        else
        {
            Cmd.Parameters.AddWithValue("@pictext, txtpictext.Text);
        }

        Conn.Open();
        Cmd.ExecuteNonQuery();

是否要使用三元运算符?:

Cmd.Parameters.AddWithValue("@pictext, string.IsNullOrEmpty(txtpictext.Text) 
  ? DBNull.Value 
  : txtpictext.Text);

Conn.Open();
Cmd.ExecuteNonQuery();

就像这样。

您想使用三元运算符吗?:

Cmd.Parameters.AddWithValue("@pictext, string.IsNullOrEmpty(txtpictext.Text) 
  ? DBNull.Value 
  : txtpictext.Text);

Conn.Open();
Cmd.ExecuteNonQuery();
string assignedValue = string.Empty;
assignedValue = string.IsNullOrEmpty(txtpictext.Text) ? DBNull.Value : txtpictext.Text ;
Cmd.Parameters.AddWithValue("@pictext", assignedValue);
Conn.Open();
Cmd.ExecuteNonQuery();
就像这样。

所以你知道有一个“内联c#if”,但很难使用或什么?重复的,还有很多很多很多。所以你知道有一个“内联c#if”,但很难使用或什么?重复的,还有很多很多很多很多。
string assignedValue = string.Empty;
assignedValue = string.IsNullOrEmpty(txtpictext.Text) ? DBNull.Value : txtpictext.Text ;
Cmd.Parameters.AddWithValue("@pictext", assignedValue);
Conn.Open();
Cmd.ExecuteNonQuery();