title: Difference between nameof and typeof
body: <p>Correct me if I am wrong, but doing something like</p>
  
  <pre><code>var typeOfName = typeof(Foo).Name;
  </code></pre>
  
  <p>and </p>
  
  <pre><code>var nameOfName = nameof(Foo);
  </code></pre>
  
  <p>should give you exactly the same output. One of the understandable reasons according to this source: <a href="https://msdn.microsoft.com/en-us/library/dn986596.aspx" rel="noreferrer">https://msdn.microsoft.com/en-us/library/dn986596.aspx</a> is that </p>
  
  <blockquote>
    <p>"Using nameof helps keep your code valid when renaming definitions"</p>
  </blockquote>
  
  <p>If you want to get the class instance as string it is <strong>not</strong> possible to do something like that:</p>
  
  <pre><code>var fooInstance = new Foo();
  var nameOfName = nameof(fooInstance);
  </code></pre>
  
  <p>however, you can do something like:</p>
  
  <pre><code>static string GetName&lt;T&gt;(T item) where T : class 
  {
    return typeof(T).GetProperties()[0].Name;
  }
  var typeOfName2 = GetName(new { fooInstance });
  </code></pre>
  
  <p>In both cases (<code>typeof</code> and <code>nameof</code>) a refactoring is possible, so I dont see any other reason for reinventing another higher level keyword, such as <code>nameof</code>, to perform something that already exists. Are there any differences between them that I don't clearly see?</p>
  
  <p>Finally, I would appreciate if could someone point me to a reference source to have a look at the implementation of <code>nameof</code>. Does it use Reflection?</p>
  
  <p><strong>Update 1:</strong>
  Taken from <a href="https://stackoverflow.com/questions/26573102/is-nameof-evaluated-at-compile-time">here</a></p>
  
  <p><code>nameof</code> is apparently as efficient as declaring a string variable. No reflection or whatsoever!</p>
  
  <pre><code>var firstname = "Gigi";
   var varname = nameof(firstname);
   Console.WriteLine(varname); // Prints "firstname" to the console
  </code></pre>
  
  <p>When you check out the MSIL generated you will see that it is equivalent to a string declaration because an object reference to a string gets pushed to the stack using the ldstr operator:</p>
  
  <pre><code>IL_0001: ldstr "Gigi"
  IL_0006: stloc.0
  IL_0007: ldstr "firstname"
  IL_000c: stloc.1
  IL_000d: ldloc.1
  IL_000e: call void [mscorlib]System.Console::WriteLine(string)
  </code></pre>