get base class from derived class c# code example

Example 1: acess base class in c#

using System;

public class A
{
   private int value = 10;

   public class B : A
   {
       public int GetValue()
       {
           return this.value;
       }
   }
}

public class C : A
{
//    public int GetValue()
//    {
//        return this.value;
//    }
}

public class Example
{
    public static void Main(string[] args)
    {
        var b = new A.B();
        Console.WriteLine(b.GetValue());
    }
}
// The example displays the following output:
//       10

Example 2: how to get derived class from base class C#

class Base {
	[...]
}

class Derived : Base {
	[...]
}

/* if an instance of the base class is derived you will be able to
cast that instance to it's derrived class, as such: */

Base baseInstance = new Derived();
Derived derivedFromInstance = (Derived)baseInstance;