title: How to refer to an identifier without writing it into a string literal in C#?
body: <p>I often want to do this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public void Foo(Bar arg)
  {
    throw new ArgumentException("Argument is incompatible with " + name(Foo));
  }
  </code></pre>
  
  <p>Because if I change the name of Foo the IDE will refactor my error message too, what won't happen if I put the name of the method (or any other kind of <strike>member</strike> identifier) inside a string literal. The only way I know of implementing "name" is by using reflection, but I think the performance loss outweighs the mantainability gain and it won't cover all kinds of identifiers.</p>
  
  <p>The value of the expression between parenthesis could be computed at compile time (like typeof) and optimized to become one string literal by changing the language specification. Do you think this is a worthy feature?</p>
  
  <p><strong>PS:</strong> The first example made it look like the question is related only to exceptions, but it is not. Think of every situation you may want to reference a <strike>type member</strike> identifier. You'll have to do it through a string literal, right?</p>
  
  <p>Another example:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[RuntimeAcessibleDocumentation(Description="The class " + name(Baz) +
    " does its job. See method " + name(DoItsJob) + " for more info.")]
  public class Baz
  {
    [RuntimeAcessibleDocumentation(Description="This method will just pretend " +
      "doing its job if the argument " + name(DoItsJob.Arguments.justPretend) +
      " is true.")]
    public void DoItsJob(bool justPretend) 
    {
      if (justPretend)
        Logger.log(name(justPretend) + "was true. Nothing done.");
    }
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong> this question was posted before C# 6, but may still be relevant for those who are using previous versions of the language. If you are using C# 6 check out the <code>nameof</code> operator, which does pretty much the same thing as the <code>name</code> operator in the examples above.</p>