How to: Inherit from a Class

This example defines the Circle and Rectangle classes, which both inherit from the Shape class, and the Square class, which inherits from the Rectangle class.

Example

public class Shape
{
    // Definitions of properties, methods, fields, and events.
}
public class Circle : Shape
{
    // Specialized properties, methods, fields, events for Circle.
}
public class Rectangle : Shape
{
    // Specialized properties, methods, fields, events for Rectangle.
}
public class Square : Rectangle
{
    // Specialized properties, methods, fields, events for Square.
}

Compiling the Code

  • Create a new console application.

  • Copy the code and paste it right before or after the Program class declaration.

Robust Programming

Make sure the class you want to inherit is not sealed.

See Also

Concepts

C# Language Primer

Other Resources

Visual C# Express

Change History

Date

History

Reason

September 2008

Modified the instructions in the "Compiling the Code" section.

Customer feedback.