title: string.IsNullOrEmpty() Doesn't Seem to Work on a String within a Class within a Class
body: <p>First off, I think I know what's going on, but I thought I'd bring this issue up here for some discussion and see if anyone has an "answer" to this other than what I'm thinking.  Because, it doesn't completely make sense to me.</p>
  
  <p>What I found is that when creating a error log for exceptions, I was doing this and it wasn't working:</p>
  
  <pre><code>catch( Exception ex )
  {
     LogException( ex.Message );
     if ( !string.IsNullOrEmpty( ex.InnerException.Message ) )
     {
        LogInnerException( ex.InnerException.Message );
     }
  }
  </code></pre>
  
  <p>and lo and behold, when I ran this I'd often get a NullReferenceException.  Huh?</p>
  
  <p>I'm checking for null, right?</p>
  
  <p>now, I have to use this:</p>
  
  <pre><code>   if ( ex.InnerException != null &amp;&amp; !string.IsNullOrEmpty( ex.InnerException.Message ) 
  </code></pre>
  
  <p>but that seems counter-intuitive and also counter productive.  Because, heck, if I do this:</p>
  
  <pre><code>   if ( !string.IsNullOrEmpty( null ) )
  </code></pre>
  
  <p>That doesn't give me any problems at all.  And if ex.InnerException is null, then certainly ex.InnerException.Message is null, right?</p>
  
  <p>Apparently not. </p>
  
  <p>I wrote a complete console app that reproduces this.  If you</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace stringisnullorempty
  {
      class Program
      {
          static void Main( string[] args )
          {
              if ( !string.IsNullOrEmpty( null ) )
              {
                  Console.WriteLine( "Ha ha ha, right...." );
              }
  
              MyBClass bClass = new MyBClass();
              bClass.BClassName = "Some B Class Name";
              if ( !string.IsNullOrEmpty( bClass.AClass.AString ) ) //&lt;== Exception occurs here.
              {
                  Console.WriteLine( bClass.AClass.AString );
              }
          }
      }
  
      public class MyAClass
      {
          private string aString;
          public string AString
          {
              get
              {
                  return aString;
              }
              set
              {
                  aString = value;
              }
          }
  
          private int aValue;
          public int AValue
          {
              get
              {
                  return aValue;
              }
              set
              {
                  aValue = value;
              }
          }
  
          public MyAClass() { }
      }
  
      public class MyBClass
      {
          private MyAClass aClass;
          public MyAClass AClass
          {
              get
              {
                  return aClass;
              }
              set
              {
                  aClass = value;
              }
          }
  
          private string bClassName;
          public string BClassName
          {
              get
              {
                  return bClassName;
              }
              set
              {
                  bClassName = value;
              }
          }
          public MyBClass() { }
      }
  }
  </code></pre>
  
  <p>What I think is happening is that the code processes ex.InnerException.Message before trying to process the IsNullOrEmpty.  Since ex.InnerException is null, we get an exception trying to access ex.InnerException.Message.</p>
  
  <p>I'm wondering though, do I need the full check?  Will the ex.InnerException != null be enough.  If we have an inner exception, will we always have a message associated with it?</p>
  
  <p>Thanks.</p>