title: How to handle null in list<> when using linq
body: <p>I found this very handy little piece of code to build a list of comma separated values using the property of a given class stored in a list (Just google ToComma link - article is on codeproject).</p>
  
  <p>I've modified it to handle a specific separator and it works great, but sometimes, some of the properties for a given object will contain a null value and I'm wondering if it is possible to expand this code to handle the null and replace it by a default value based on the property type i.e. if string, replace null by string.empty, if numeric, replace it by 0, if bool replace it by false.</p>
  
  <p>If the property in question that I'm trying to build of comma separated value happen to contain a null value, it will throw an error i.e. object reference not set to an instance...</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public static string ToSeparator&lt;T, TU&gt;(this IEnumerable&lt;T&gt; source, 
  Func&lt;T, TU&gt; func, string separator)
  {
      return string.Join(separator, source.Select(s =&gt; func(s).ToString()).ToArray());
  }
  </code></pre>
  
  <p>Assuming you have a list i.e. List persons, where the Person object is made up of FirstName, Surname and Age for example, you can then simply call it:</p>
  
  <pre><code>persons.ToSeparator(m =&gt; m.FirstName, "-"); 
  persons.ToSeparator(m =&gt; m.Age, "-"); 
  </code></pre>
  
  <p>Can this be done using the above code? If it can't be done using the above code, what would be the best way to handle this using link and/or lambda expression.</p>
  
  <p>Thanks.</p>