title: Access base class properties from a derived class
body: <p>So I have come across a problem I have created a object of a class, this class also have a derived class from where I need to acces these properties. I actually already have two solutions to this problem, which I will present here later. I am not sure if this will be considered a valid question or just a code review since I already have solutions to this specific problem. I guess I am looking for a better solution. Well lets jmp in to the question:</p>
  
  <p>So in my base class I have this code:</p>
  
  <pre><code>public class MyBaseClass : IMyInterface
  {
      public string myProperty { get; set; }
      public string myProperty2 { get; set; }
      public MyBaseClass(string prop1, string prop2)
      {
         myProperty = prop1;
         myProperty2 = prop2;
      }
  
      public virtual void PrintProps()
      {
         Console.WriteLine(myProperty + " " + myProperty2);
      }
  }
  </code></pre>
  
  <p>I if now from Main create an object of this class with the parameters "Hello" and "World!" and then I call the PrintProps method I should see "Hello World! on my screen, which I do. </p>
  
  <p>Code:</p>
  
  <pre><code>MyBaseClass myBase = new MyBaseClass("Hello!", "World");
  myBase.PrintProps();
  </code></pre>
  
  <p>What I would like to do now is to have a Child class that have the exact same method which should print out the properties that is created in my parent class.</p>
  
  <p>Something like this I would want to do:</p>
  
  <pre><code>public class MyChildClass: MyBaseClass
  {
      public override void PrintProps()
      {
          Console.WriteLine(myProperty + " " + myProperty2);
      }
  }
  </code></pre>
  
  <p>And then call it from Main:</p>
  
  <pre><code>MyChildClass myChild = new MyChildClass ();
  myChild.PrintProps();
  </code></pre>
  
  <p>When I call the childclass's printProp method I should expect "Hello World!" to be printed on the screen, this doesn't work obviously. There is two ways to fix this which I am aware of. First way to fix this (Not the most object oriented way):</p>
  
  <pre><code>public class MyChildClass
  {
      MyBaseClass myBase = default;
      public MyChildClass(MyBaseClass myBase)
      {
          this.myBase = myBase;
      }
  
      public void PrintProps()
      {
          Console.WriteLine(myBase.myProperty + " " + myBase.myProperty2);
      }
  }
  </code></pre>
  
  <p>and then call it in main like this:</p>
  
  <pre><code>MyBaseClass myBase = new MyBaseClass("Hello!", "World");
  var myChild = new MyChildClass(myBase);
  myChild.PrintProps();
  </code></pre>
  
  <p>This works like a charm since I am passing in the created object of the base class but with this way I am not using any inheritance, which I think is a bad way to do it. </p>
  
  <p>The other way to fix this problem would be to call the base class constructor in my child class like this:</p>
  
  <pre><code>public class MyChildClass : MyBaseClass
  {
      public MyChildClass() : base("Hello", "World!") { }
      public override void PrintProps()
      {
          Console.WriteLine(myProperty + " " + myProperty2);
      }
  }
  </code></pre>
  
  <p>and then call it from main like this:</p>
  
  <pre><code>MyBaseClass myBase = new MyBaseClass("Hello!", "World");
  MyChildClass myChild = new MySubClass();
  myChild.PrintProps();
  </code></pre>
  
  <p>This also solves the problem, and this way I am using inheritance and polymorphism. But as you see I am creating one object of the base class in Main, and Another object in the derived class. But since both objects is identical I figure it would be unnecessary to create two objects.
  So in some way I would want to use the object that is created in Main in my derived class, without passing the object in as a argumnet. I want to do it using Inheritance and polymorphism. So to sum this up is it possible to not "duplicate" the object of the base class when I am inheriting the base class and using it in my child class? Or will I have to create one object in main and then another one in my childclass? I think I could use the PropertyInfo class in some way to fix this problem, but I am not very experienced in this so I might just bee asking for the impossible. A lot to read I know. Any advice will be appreciated :).</p>
  
  <p>Edit: I need to create the baseclass object in Main because I am calling both the base class PrintProp method and the child class PrintProp method in Main and I want the same result with both methods. 
  Code:</p>
  
  <pre><code>MyBaseClass myBase = new MyBaseClass("Hello", "World!");
  myBase.PrintProps(); //Expected "Hello World!" to be printed to the screen
  
  MyChildClass myChild = new MyChildClass();
  myChild.PrintProps(); //Also expecting "Hello World!" to be printed to the screen
  </code></pre>
  
  <p>Edit:
  To be more specific I want to create an object of the base class in the Main method. And then I want to access the properties of this object from the child class, without passing the base class as an argument to the child class and without Calling the base classs constructor from my child class and thus create a second object of the base class.</p>
  
  <p>So if I from Main pass in "123" in the base class constructor I would then want to be able to access this property from my child class. So in my child class the property should also be "123". I do NOT want to call the base class constructor in my child class like this:</p>
  
  <pre><code> public class MyChildClass : MyBaseClass
   {
      public MyChildClass() : base("123") { }
   }
  </code></pre>
  
  <p>I think PropertyInfo is what I am looking for but I am not sure since I have never used that class. Like I said I might (and most possibly is) asking for the impossible.</p>