title: C# – How to override GetHashCode for List<T> to calculate ETag with T being a record
body: <p>For a NetCore Web API <code>GET</code> method I need to caluclate the ETag for a returned <code>List&lt;T&gt;</code>. T is the DTO in the form of a <code>record</code> that holds only primitive types.</p>
  <p>I wanted to calculate a hash of the list. I was searching for information about how <code>GetHashCode()</code> is implemented, but couldn't find any information. The documentation of <a href="https://docs.microsoft.com/de-de/dotnet/api/system.object.gethashcode?view=net-6.0" rel="nofollow noreferrer">object.GetHashCode()</a>
  doesn't state any information about lists or collections. By the results of the code I observed that on each run the same list data creates a different hash code. I concluded that <code>GetHashCode()</code> uses the pointer values for reference type items.</p>
  <p><code>GetHashCode()</code> of <code>record</code> calculates the hash code per member value. Therefore I created the list hash code by looping over the list items:</p>
  <pre><code>List&lt;GetGroupsDTO&gt; dtoList = commandResult.Value;
  int hash = 17;
  foreach(GetGroupsDTO dto in dtoList)
  {
     hash = hash * 23 + dto.GetHashCode();
  }
  string eTagPayload = hash.ToString().SurroundWithDoubleQuotes();
  </code></pre>
  <p>I don't want to do this for every <code>List&lt;T&gt;</code>, of course. I thought to override <code>GetHashCode()</code>, but I'm struggling with it. I don't know how to override it for the generic List. I could derive a new class DTOList where I can override <code>GetHashCode()</code>. But this leads to more complexity in other places. Since the result of an EFCore Set query fills the List I would need a custom converter and then a custom serializer to return the List in Web API.</p>
  <p>Therefore I wonder if I rather should create an extension method for List or just a function that takes List as an argument. Is there any other option to calculate the ETag? How can I calculate the ETag for a list of DTO objects efficently?</p>