title: How to make Generic Repository able to automatically infer the type of primary key?
body: <p>The following code already works but I think specifying <code>int</code> in the following</p>
  <pre><code>var repo = new Repository&lt;Student, int&gt;(students);
  </code></pre>
  <p>is a bit redundant. I want to simplify as follows:</p>
  <pre><code>var repo = new Repository&lt;Student&gt;(students);
  </code></pre>
  <p>How should I modify the <code>IRepository</code> and <code>Repository</code> below to achieve what I want?</p>
  <p><strong>Minimal Working Example:</strong></p>
  <pre><code>class Entity&lt;TKey&gt;
  {
      public TKey Id { get; set; } = default!;
  }
  
  
  interface IRepository&lt;TEntity, TKey&gt; where TEntity : Entity&lt;TKey&gt;
  {
      IEnumerable&lt;TEntity&gt; GetAll();
  }
  
  class Repository&lt;TEntity, TKey&gt; : IRepository&lt;TEntity, TKey&gt; where TEntity : Entity&lt;TKey&gt;
  {
      private readonly IEnumerable&lt;TEntity&gt; data;
      public Repository(IEnumerable&lt;TEntity&gt; data) =&gt; this.data = data;
      public IEnumerable&lt;TEntity&gt; GetAll() =&gt; data;
  }
  
  
  class Student : Entity&lt;int&gt;
  {
      public string Name { get; set; } = default!;
  }
  
  class Program
  {
      static void Main()
      {
          var students = new Student[]
          {
             new Student { Name=&quot;Andy&quot;},
             new Student { Name=&quot;Bob&quot;},
             new Student { Name=&quot;Cindy&quot;}
          };
  
          var repo = new Repository&lt;Student, int&gt;(students);
      }
  }
  </code></pre>
  <p>Note: My entities can have primary keys of type <code>string</code>, <code>int</code>, <code>Guid</code>, etc.</p>