How to create a subclass in C#?
Do you mean this?
public class Foo
{}
public class Bar : Foo
{}
In this case, Bar is the sub class.
Here is an example of writing a ParentClass and then creating a ChildClass as a sub class.
using System;
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}
Output:
Parent Constructor. Child Constructor. I'm a Parent Class.
Rather than rewriting yet another example of .Net inheritance I have copied a decent example from the C Sharp Station website.