title: Generic identity function for use with type inference
body: <p>I was wondering if it is possible, as my 5 minutes of experimentation proved fruitless.</p>
  
  <p>I hoped it would be as easy as:</p>
  
  <pre><code>T Identity&lt;T&gt;(T t) { return t; }
  </code></pre>
  
  <p>But this fails to compile on generic methods taking Func parameters. Eg OrderBy. Even specifying type parameters (which is exactly what I want to avoid!), it fails to compile.</p>
  
  <p>Next I tried something I thought would work:</p>
  
  <pre><code>Func&lt;T, R&gt; MakeIdentity&lt;T, R&gt;()
  {
    return (T t) =&gt; (R)(object)t;
  }
  </code></pre>
  
  <p>Also no go  :(  (this compiles when applying type parameters, again, not what I want)</p>
  
  <p>Has anyone had luck making such a thing?</p>
  
  <p>UPDATE: please dont say: x => x, I know that, it's obvious!  I am asking for a function, not an expression  :)</p>
  
  <p>UPDATE 2: When I refer to identity, I mean in the functional sense, where the function simply returns the same object that you passed to it. It is probably in every functional language I have come across, but those do not use static typing. I am wondering how to do this (if possible) with generics. Just for fun!</p>
  
  <p>UPDATE 3: Here's a partial 'solution' based on the 2nd idea:</p>
  
  <pre><code>Expression&lt;Func&lt;T, T&gt;&gt; MakeIdentity&lt;T&gt;()
  {
    return t =&gt; t;
  }
  
  void Foo(string[] args)
  {
    var qargs = args.AsQueryable();
    var q = qargs.OrderBy(MakeIdentity&lt;string&gt;());
    ...
  }
  </code></pre>
  
  <p>I dont think anything more than this will be possible.</p>