title: Fastest way to check if a string can be parsed
body: <p>I am parsing CSV files to lists of objects with strongly-typed properties.  This involves parsing each string value from the file to an <code>IConvertible</code> type (<code>int</code>, <code>decimal</code>, <code>double</code>, <code>DateTime</code>, etc) using <code>TypeDescriptor</code>.  </p>
  
  <p>I am using a <code>try catch</code> to handle situations when parsing fails.  The exact details of where and why this exception occurs is then logged for further investigation.  Below is the actually parsing code:</p>
  
  <pre><code>try
  {
      parsedValue = TypeDescriptor.GetConverter(type).ConvertFromString(dataValue);
  }
  catch (Exception ex)
  {
      // Log failure
  }
  </code></pre>
  
  <p><strong>Problem:</strong></p>
  
  <p>When values are successfully parsed, the process is quick.  When parsing data with lots of invalid data, the process can take thousands of times slower (due to catching the exception).</p>
  
  <p>I've been testing this with parsing to <code>DateTime</code>.  These are the performance figures:</p>
  
  <ul>
  <li>Successful parsing: average of <strong>32 ticks</strong> per parse</li>
  <li>Failed parsing: average of <strong>146296 ticks</strong> per parse</li>
  </ul>
  
  <p>That's more than 4500 times slower.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>Is it possible for me to check to see if a string value can be successfully parsed without having to use my expensive <code>try catch</code> method?  Or perhaps there is another way I should be doing this?</p>
  
  <p><strong>EDIT:  I need to use <code>TypeDescriptor</code> (and not <code>DateTime.TryParse</code>) because the type is determined at runtime.</strong></p>