Checking for string contents? string Length Vs Empty String

前端 未结 13 896
野趣味
野趣味 2020-12-17 10:14

Which is more efficient for the compiler and the best practice for checking whether a string is blank?

  1. Checking whether the length of the string == 0
相关标签:
13条回答
  • 2020-12-17 10:53

    String.IsNullOrEmpty() only works on .net 2.0 and above, for .net 1/1.1, I tend to use:

    if (inputString == null || inputString == String.Empty)
    {
        // String is null or empty, do something clever here. Or just expload.
    }
    

    I use String.Empty as opposed to "" because "" will create an object, whereas String.Empty wont - I know its something small and trivial, but id still rather not create objects when I dont need them! (Source)

    0 讨论(0)
提交回复
热议问题