title: C# error CS0165: Use of unassigned local variable - ignoring logic and out reference
body: <p>After searching around I cant seem to locate why the C# compiler is complaining that the local variable <b>dteDest</b> is unassigned in the line</p>
  
  <pre><code>if (dteSrc == dteDest) {
  </code></pre>
  
  <p>The error goes away if I replace the line </p>
  
  <pre><code>DateTime dteSrc, dteDest;
  </code></pre>
  
  <p>with</p>
  
  <pre><code>DateTime dteSrc, dteDest = DateTime.MinValue;
  </code></pre>
  
  <p>As far as I can see the code will never reach the comparison line if dteDest is not initialised by the DateTime.TryParse which it is an out parameter for.</p>
  
  <p>My logic is:</p>
  
  <ol>
  <li>If currentDataObj is null then booHaveOrigDate is false and the first <b>if</b> fails</li>
  <li>If currentDataObj is not null but cant be converted to a DateTime then booHaveOrigDate is false and the first <b>if</b> fails</li>
  <li>DateTime.TryParse will return false if it cant convert to a DateTime this along with the &amp;&amp; means that dteDest will never be used.</li>
  </ol>
  
  <h2>Simple Sample Code</h2>
  
  <pre><code>void StrangeLogic(object srcData, object currentDataObj) {
     DateTime dteSrc, dteDest;
  
     bool booHaveNewDate = DateTime.TryParse(srcData.ToString(), out dteSrc);
     bool booHaveOrigDate = (currentDataObj != null) 
                            &amp;&amp; DateTime.TryParse(currentDataObj.ToString(), out dteDest);
  
     if (booHaveNewDate &amp;&amp; booHaveOrigDate) {
        if (dteSrc == dteDest) { 
            // Get a "use of unassignned local variable 'dteDest' 
            // unless dteDest = DateTime.MinValue beforehand
        }
     }
  }
  </code></pre>
  
  <p>Also if I change the line</p>
  
  <pre><code>bool booHaveNewDate = DateTime.TryParse(srcData.ToString(), out dteSrc);
  </code></pre>
  
  <p>to the following</p>
  
  <pre><code>bool booHaveNewDate = (srcData != null) &amp;&amp; DateTime.TryParse(srcData.ToString(), out dteSrc);
  </code></pre>
  
  <p>then the compiler complains that srcDate is not assigned as well.</p>
  
  <p>Could anyone point me in the right direction to what I am missing - I dont mean about parameter checking etc I am concerned with why the compiler logic seems to be fooled by the use of a common TryParse function?</p>
  
  <h2>Additional Info</h2>
  
  <p>Even expanding out the logic still gives the same error (use of unassigned local variable)</p>
  
  <pre><code>bool booHaveOrigDate;
  if (currentDataObj != null) 
     booHaveOrigDate = DateTime.TryParse(currentDataObj.ToString(), out dteDest); 
  else 
     booHaveOrigDate = false;
  
  if (booHaveOrigDate) {
      if (dteSrc == dteDest) {
  </code></pre>
  
  <p>It appears that it is whatever the compiler does with the null checking (currentDataObj != null) that prevents it from correctly determing the dteDest wont be accessed unless assigned</p>
  
  <p>Change it to this code and no problems (aside from the possible .ToString() on a null object</p>
  
  <pre><code>bool booHaveOrigDate = DateTime.TryParse(currentDataObj.ToString(), out dteDest); 
  if (booHaveOrigDate) {
      if (dteSrc == dteDest) {
  </code></pre>