title: Implicit and explicit typing with C# 6 nameof
body: <p>One of the handiest new features in C# 6 is <a href="https://msdn.microsoft.com/en-us/library/dn986596.aspx"><code>nameof</code></a>, which allows the programmer to effectively eliminate the use of <a href="https://en.wikipedia.org/wiki/Magic_string">magic strings</a>.</p>
  
  <p>Per the documentation, <code>nameof</code> returns a string:</p>
  
  <blockquote>
    <p>Used to obtain the simple (unqualified) string name of a variable, type, or member.</p>
  </blockquote>
  
  <p>That works just fine with explicit typing in the following code example:</p>
  
  <pre><code>string magicString = nameof(magicString);
  </code></pre>
  
  <p>However, when using implicit typing with the <a href="https://msdn.microsoft.com/en-us/library/bb383973.aspx"><code>var</code> keyword</a>:</p>
  
  <pre><code>var magicString = nameof(magicString);
  </code></pre>
  
  <p>the compiler throws an error:</p>
  
  <blockquote>
    <p>Cannot use local variable 'magicString' before it is declared</p>
  </blockquote>
  
  <p>I then did some more experimenting with the C# Interactive window available in Visual Studio. Again, the first example worked fine, but the second example threw a different error this time:</p>
  
  <blockquote>
    <p>error CS7019: Type of 'magicString' cannot be inferred since its initializer directly or indirectly refers to the definition.</p>
  </blockquote>
  
  <p>The <code>nameof</code> expression clearly returns a string, so why can't the compiler implicitly type it when being used with the initialized variable?</p>