C# if-null-then-null expression

前端 未结 6 1928
误落风尘
误落风尘 2020-12-01 02:56

Just for curiosity/convenience: C# provides two cool conditional expression features I know of:

string trimmed = (input == null) ? null : input.Trim();
         


        
6条回答
  •  [愿得一人]
    2020-12-01 03:04

    Currently we can only write an extension method if you don't want to repeat yourself, I'm afraid.

    public static string NullableTrim(this string s)
    {
       return s == null ? null : s.Trim();
    }
    

提交回复
热议问题