string.IsNullOrEmpty() vs string.NotNullOrEmpty()

前端 未结 14 942
遇见更好的自我
遇见更好的自我 2021-02-01 02:56

I\'m curious if any developers use string.IsNullOrEmpty() more often with a negative than with a positive

e.g.

if (!string.IsNullOrEmpty())
相关标签:
14条回答
  • 2021-02-01 03:58

    I prefer the extension method:

    public static class StringExtensions
    {
        public static bool IsNullOrEmpty(this string value)
        {
            return string.IsNullOrEmpty(value);
        }
    }
    

    I find it reads better to say:

    if(myValue.IsNullOrEmpty())
    

    or

    if(!myValue.IsNullOrEmpty())
    
    0 讨论(0)
  • 2021-02-01 03:59

    C# naming conventions dictate that your expressions should be in the positive such as "Is..." and not "IsNot..."

    EDIT: Typically, I use it when doing error checking and input validation at the beginning of a method and raise an exception if the parameter is null or empty.

    if (string.IsNullOrEmpty(myParameter))
    {
    throw new ....
    }

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