C#: Inheritance Cheat Sheet

<access_modifier> class <base_class_name>
{
    // Base class Implementation
}
 
<access_modifier> class <derived_class_name> : <base_class_name>
{
    // Derived class implementation
}

Simple example of implementing inheritance in C#

public class X
{
    public void GetDetails()
    {
        // Method implementation
    }
}
public class Y : X
{
    // your class implementation
}
class Program
{
    static void Main(string[] args)
    {
        Y y = new Y();
        y.GetDetails();           
    }
}

Example

class Dog 
{
  public string type = "Poodle";  
  public void sound()             
  {                    
    Console.WriteLine("woof, woof!");
  }
}

class Poodle : Dog  
{
  public string typeName = "Toy";  
}

class Program
{
  static void Main(string[] args)
  {
    Poodle myPoodle = new Poodle();
    myPoodle.sound();
    Console.WriteLine(myPoodle.typeName + " " + myPoodle.type);
  }
}

=> output

woof, woof!
Toy Poodle

Leave a Reply

Your email address will not be published. Required fields are marked *