简体   繁体   中英

Why gettype for two different objects of same type has same reference

public class Test
{
    class A
    {
        public int i;
        public A(int x)
        {
            i = x;
        }
        public A()
        {

        }
        public override bool Equals(Object obj)
        {
            A a = (A)obj;
            if (this.i == a.i)
                return true;
            else
                return false;
        }        
    }
    static void Main()

    {
        A a = new A(1);
        A b = new A();
        Type t =typeof(A);
        Type t1 = b.GetType();
        Console.WriteLine(ReferenceEquals(t,t1));
    }
}

This returns true(that implies that they have same reference)just like string concepts strings returns true as they have interned pool what does this have. Thanks in advance

It returns the same instance because it is documented to so:

For two objects x and y that have identical runtime types, Object.ReferenceEquals(x.GetType(),y.GetType()) returns true .

The exact implementation details on how they implemented this is undocumented.

If your question is really "how did they implement this so that it always returns the same instance" then that part is not documented.

I would assume it is relatively safe to say they have a lookup data structure containing these objects but exactly how they did this, that is undocumented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM