title: ReferenceEquals with casting challenge. Casting to child type make ReferenceEquals return false
body: <p>Casting does not make copies of objects. More concretely <a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.cast?view=net-6.0" rel="nofollow noreferrer">IEnumerable.Cast</a>, according to <a href="https://stackoverflow.com/questions/9198876/does-enumerable-castt-copy-objects">this</a> resource, which makes total sense. However for this app, find <a href="https://dotnetfiddle.net/eAx2oW" rel="nofollow noreferrer">fiddle</a>, reference equals is false if I change the log at the end from the <code>Cast&lt;BaseClass&gt;</code> to <code>Cast&lt;DerivedClass1&gt;</code>. Check logs at the main program.</p>
  <p><code>are equal:  True areEqualRef: True areEqualRef: False</code></p>
  <p>Posting the code, just in case, omit it of you get my point playing around with the fiddle :)</p>
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace ConsoleApp17
  {
      public class BaseClass {
          string name = &quot;I am base&quot;;
          public Type type;
          public enum Type { 
              a,b,c,d
          }
          public BaseClass(Type type) {
              this.type = type;
          }
      }
  
      public class DerivedClass1 : BaseClass
      {
          string name = &quot;I am derivedA&quot;;
  
          public DerivedClass1(): base(Type.a) {
          
          }
      }
  
  
      public class DerivedClass2 : BaseClass
      {
          string name = &quot;I am derivedB&quot;;
          public DerivedClass2() : base(Type.b)
          {
  
          }
      }
  
      public class Foo
      {
          public Dictionary&lt;BaseClass.Type, List&lt;BaseClass&gt;&gt; dict = new Dictionary&lt;BaseClass.Type, List&lt;BaseClass&gt;&gt;();
  
          public Foo() {
  
              dict[BaseClass.Type.a] = new List&lt;BaseClass&gt;();
              dict[BaseClass.Type.b] = new List&lt;BaseClass&gt;();
              dict[BaseClass.Type.c] = new List&lt;BaseClass&gt;();
              dict[BaseClass.Type.d] = new List&lt;BaseClass&gt;();
  
              AddItem(new DerivedClass1());
              AddItem(new DerivedClass1());
  
              AddItem(new DerivedClass2());
              AddItem(new DerivedClass2());
              AddItem(new DerivedClass2());
          }
  
          public IEnumerable&lt;T&gt; GetEnumByType&lt;T&gt;(BaseClass.Type type) where T : BaseClass
          {
              if (dict.ContainsKey(type))
              {
                  if (type == BaseClass.Type.a)
                  {
                      Console.WriteLine($&quot;are equal:  { object.ReferenceEquals(dict[type].Cast&lt;T&gt;(), dict[BaseClass.Type.a])}&quot;);
                  }
  
                  return dict[type].Cast&lt;T&gt;();
              }
              return null;
          }
  
          public void AddItem&lt;T&gt;(T item) where T : BaseClass
          {
              dict[item.type].Add(item);
          }
      }
      class Program
      {
          static void Main(string[] args)
          {
              Foo foo = new Foo();
              IEnumerable myList = foo.GetEnumByType&lt;BaseClass&gt;(BaseClass.Type.a);
  
              Console.WriteLine($&quot;areEqualRef: {object.ReferenceEquals(foo.dict[BaseClass.Type.a].Cast&lt;BaseClass&gt;(), foo.dict[BaseClass.Type.a])}&quot;);
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  <p><strong>Update:</strong>
  Updated the fiddle with both logs to avoid the need of copy/pasting</p>