One liner for If string is not null or empty else

后端 未结 6 1454
孤城傲影
孤城傲影 2020-12-08 18:03

I usually use something like this for various reasons throughout an application:

if (String.IsNullOrEmpty(strFoo))
{
     FooTextBox.Text = \"0\";
}
else
{
          


        
6条回答
  •  有刺的猬
    2020-12-08 19:01

    You can achieve this with pattern matching with the switch expression in C#8/9

    FooTextBox.Text = strFoo switch
    {
        { Length: >0 } s => s, // If the length of the string is greater than 0 
        _ => "0" // Anything else
    };
    

提交回复
热议问题