Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this (null || !TryParse) conditional result in "use of unassigned local variable"?
    text
    copied!<p>The following code results in <em>use of unassigned local variable "numberOfGroups"</em>:</p> <pre><code>int numberOfGroups; if(options.NumberOfGroups == null || !int.TryParse(options.NumberOfGroups, out numberOfGroups)) { numberOfGroups = 10; } </code></pre> <p>However, this code works fine (though, <a href="http://en.wikipedia.org/wiki/ReSharper" rel="noreferrer">ReSharper</a> says the <code>= 10</code> is redundant):</p> <pre><code>int numberOfGroups = 10; if(options.NumberOfGroups == null || !int.TryParse(options.NumberOfGroups, out numberOfGroups)) { numberOfGroups = 10; } </code></pre> <p>Am I missing something, or is the compiler not liking my <code>||</code>?</p> <p>I've narrowed this down to <code>dynamic</code> causing the issues (<code>options</code> was a dynamic variable in my above code). The question still remains, <strong>why can't I do this</strong>?</p> <p>This code <em>doesn't</em> compile:</p> <pre><code>internal class Program { #region Static Methods private static void Main(string[] args) { dynamic myString = args[0]; int myInt; if(myString == null || !int.TryParse(myString, out myInt)) { myInt = 10; } Console.WriteLine(myInt); } #endregion } </code></pre> <p>However, this code <em>does</em>:</p> <pre><code>internal class Program { #region Static Methods private static void Main(string[] args) { var myString = args[0]; // var would be string int myInt; if(myString == null || !int.TryParse(myString, out myInt)) { myInt = 10; } Console.WriteLine(myInt); } #endregion } </code></pre> <p>I didn't realize <code>dynamic</code> would be a factor in this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload