string.IsNullOrEmpty(string) vs. string.IsNullOrWhiteSpace(string)

后端 未结 9 723
广开言路
广开言路 2020-11-28 03:02

Is use of string.IsNullOrEmpty(string) when checking a string considered as bad practice when there is string.IsNullOrWhiteSpace(string) in .NET 4.

9条回答
  •  长情又很酷
    2020-11-28 03:19

    What about this for a catch all...

    if (string.IsNullOrEmpty(x.Trim())
    {
    }
    

    This will trim all the spaces if they are there avoiding the performance penalty of IsWhiteSpace, which will enable the string to meet the "empty" condition if its not null.

    I also think this is clearer and its generally good practise to trim strings anyway especially if you are putting them into a database or something.

提交回复
热议问题