title: Check if T in Func<T,string> delegate is null before invoking?
body: <p>I am trying to figure out how to check if an object is null before it is invoked. Here is some sample code:</p>
  
  <pre><code> public class NotifyTemplate&lt;T&gt; where T : class
      {
          public Func&lt;T, string&gt; Value { get; set; }
          public string Key { get; set; }
      }
  
      public class User
      {
          public int Id { get; set; }
          public string UserName { get; set; }
  
          public Contact Contact { get; set; }
  
      }
  
      public class Contact
      {
          public string FirstName { get; set; }
          public string LastName { get; set; }
      }
  </code></pre>
  
  <p>I want check if any of the objects i am trying to invoke are null before I actually invoke the delegate. I can throw a try catch, but that seems overkill. Here is an example of my usage:</p>
  
  <pre><code>var notifyList = new List&lt;NotifyTemplate&lt;User&gt;&gt;()
              {
                  new NotifyTemplate&lt;User&gt;()
                  {
                      Value = u =&gt; u.Contact.FirstName,
                      Key = "FName"
                  },
                  new NotifyTemplate&lt;User&gt;()
                  {
                      Value = u =&gt; u.UserName,
                      Key = "UserName"
                  }
              };
  
              var user = new User()
              {
                  Id = 1,
                  UserName = "ddivita@dd.com",
  
              };
  
              foreach (var notifyTemplate in notifyList)
              {
                 //This is where I want to check if the value is null or not before I invoke.
  
  
                  var name =  notifyTemplate.Value.Invoke(user);
              }
  </code></pre>
  
  <p>As you can see the Contact object is null and not initialized in the <code>new User</code> declaration. So, inside the <code>foreach</code> statement I want to inspect the Value property and see if the object inside my delegate is null before I invoke it. </p>
  
  <p>The whole point to this is that we are setting up a notification system and to make it easier on the developer, the Func will be used to replace keys inside a notification template.</p>
  
  <p>Here is what teh temaplte might look like:</p>
  
  <pre><code>&lt;html&gt;
     &lt;body&gt;
        Hello There [FName] [LName]. Thank you for signing up using the username: [UserName].
  
     &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The developer will pass into our notification service what template they want to use and the list of NotifyTemplate</p>
  
  <p>If for some reason the contact information was not persisted correctly and we pull a User object from the database, let's assume the Contact object is null. We need to evaluate that the Contact object is null before we call invoke to get the value. </p>