0
using System;
public class A 
{
    public A()
    {
     Console.WriteLine(this.GetHashCode());
     Console.WriteLine("A Created");
    }
}
public class B : A
{
    public B()
    {
     Console.WriteLine(this.GetHashCode());
     Console.WriteLine("B Created");
    }
}
public class C
{
  public static void Main(String[] args)
  {
      B t = new B();
  }
}

Output: 1805487208 A Created 1805487208 B Created

Why i got the same address for these 2 objects. Actually these 2 objects must be created side by side . But how come we are getting the same address for these two objects???

  • 4
    There is only one object (B includes A; it is not *separate* from A). Why do you think there are two? – Matthew Watson Feb 10 '20 at 12:04
  • BTW, this question could be duplicated of https://stackoverflow.com/a/26615241/2265446 – Cleptus Feb 10 '20 at 12:09
  • @MatthewWatson ty, deleting the comment to avoid confusion – Cleptus Feb 10 '20 at 12:10
  • There are two objects getting created..one for class A, and other for class B(INTERNALLY). – Saketh Kota Feb 10 '20 at 12:10
  • @SakethKota No, there are NOT two objects being created. Only ONE object is created - the B object, which extends the A class. – Matthew Watson Feb 10 '20 at 12:16
  • *"Actually these 2 objects must be created side by side"* - why do you think that? And why do you think that there are two objects at all? – germi Feb 10 '20 at 12:17
  • @MatthewWatson Two objects are created and there is no doubt in that..Ok lemme send you a code to prove that for your clarity...i have created destructors for the two classes..when the objects scope is finished the destructors are called before respective objects get deleted. THIS MEANS AN OBJECT GETTING CREATED AND GETTING DESTROYED – Saketh Kota Feb 10 '20 at 12:25
  • using System; public class A { String s="saketh"; public A(){ // Console.WriteLine(this.GetHashCode()); Console.WriteLine(s.GetHashCode()); Console.WriteLine("A Created"); } ~A(){ Console.WriteLine("A Deleted"); } } public class B : A { public B(){ // Console.WriteLine(this.GetHashCode()); Console.WriteLine("B Created"); } ~B(){ Console.WriteLine("B Deleted"); } } public class C { public static void Main(String[] args) { B t = new B(); } } – Saketh Kota Feb 10 '20 at 12:25
  • 1
    @SakethKota, maybe you should read up on inheritance – Mox Feb 10 '20 at 12:26
  • On a side node, why did the constructor of parent class gets invoked without calling super()? is this something unique to C#? – Mox Feb 10 '20 at 12:27
  • @SakethKota Read this: https://stackoverflow.com/questions/35172738/c-net-inheritance-number-of-objects-created-in-memory – Matthew Watson Feb 10 '20 at 12:35
  • @Mox C++ also does this. If there is ONLY a parameterless constructor for a base class, it will be called automatically when you construct an instance of a derived class. If there is more than one constructor, you must choose which one to use. – Matthew Watson Feb 10 '20 at 12:36
  • 2
    Does this answer your question? [Same GetHashCode() for different objects](https://stackoverflow.com/questions/26615134/same-gethashcode-for-different-objects) – Abhishake Gupta Feb 10 '20 at 12:39
  • @ABAbhi That's a different issue. There aren't two objects, there is only one object. – Matthew Watson Feb 10 '20 at 12:39
  • 1
    What do you mean by "addresses"? Where did you check that any of these objects has an "address"? – Nico Haase Feb 10 '20 at 12:43
  • The .Net Source Code is available, but as you can see in [this answer](https://stackoverflow.com/a/27196724/2557128), addresses have nothing to do with hashcodes, and having the default `Object.GetHashCode` return the same value means you have the same object. – NetMage Feb 11 '20 at 00:55

0 Answers0