Tech Off Post

Single Post Permalink

View Thread: How to check nullness
  • User profile image
    y2k4life

    What I did was write a IsNullOrBlank in a Helper Class.

    the key is that you will want to test length because that is quicker than testing the string itself. is string == ""; sting.length == 0

    Also want to test null before lenght. The idea is that if one is true then the if stops. If the first test is for lenght on a sting that is null you will get an error. So the order of the test is important because if buffer == null the other 'or' part of the if will not execute.

    public class StringHelper
    {
        public static bool IsNullOrBlank(string testBuffer)
        {
           if(testBuffer == null || testBuffer.Length == 0)
               return true;
           else
               return false;
        }
    }

    if(!StringHelper.IsNullOrBlank(buffer))
      buffer not null
    else
      buffer is null

    if(StringHelper.IsNullOrBlank(buffer))
      buffer is null
    else
      buffer is not null


    - g