Category Archives: OOP

Draw a scrolling family tree in C#

The example Draw a family tree in C# shows how to draw a family tree. Unfortunately if the tree is too big, you can’t make the form big enough to display it all. One solution would be to use smaller … Continue reading

Posted in algorithms, classes, generic, graphics, OOP | Tagged , , , , , , , , , , , , , , , , , , , , , | 1 Comment

Clone lists and arrays of objects in C#

This is the last post I’ll write about clones, at least for a while. Some collection classes can already make clones. For example, an array can clone itself, although the result is always a shallow clone. This example shows how … Continue reading

Posted in algorithms, OOP, syntax | Tagged , , , , , , , , , , , , , | 1 Comment

Clone serializable objects in C#

If a class is serializable, then you can create a deep clone of an object from that class by serializing it and the deserializing it. This example uses the following code to define a generic extension method that clones objects … Continue reading

Posted in algorithms, OOP, syntax | Tagged , , , , , , , , , , , , , | 5 Comments

Use MemberwiseClone the ICloneable interface in C#

The example Clone objects in C# explains how to give a class a Clone method that makes copies of its objects. This example makes two changes: it implements the ICloneable interface and it uses MemberwiseClone to simplify the cloning code. … Continue reading

Posted in algorithms, OOP, syntax | Tagged , , , , , , , , , , , , , , , | Leave a comment

Clone objects in C#

A clone of an object is a copy of that object. There are two kinds of clones: shallow and deep. A shallow clone shares all of the same values as the original object, even if some of those values are … Continue reading

Posted in algorithms, OOP, syntax | Tagged , , , , , , , , , , , , | Leave a comment

Provide multiple inheritance in C#

Sometimes you might want a class to inherit from more than one parent class. For example, you might define a Vehicle class that has vehicle properties such as MaxSpeed, and a Domicile class with house-like properties such as SquareFeet. You … Continue reading

Posted in inheritance, interfaces, OOP | Tagged , , , , , , , , , | 1 Comment

Define and implement an interface in C#

(This example doesn’t do anything. It just shows how to define an interface.) An interface defines properties, methods, and events for a class but doesn’t provide an implementation for them. A class that implements the interface must provide code for … Continue reading

Posted in interfaces, OOP | Tagged , , , , , , , , , | Leave a comment

Override a parent class method in C#

One of the coolest features of object-oriented programming is virtual methods. If a parent class declares a method as virtual, a child class can provide a new implementation for the method. When the program calls the method for a child … Continue reading

Posted in classes, OOP | Tagged , , , , , , , , , , , , | 1 Comment

Make an initializing constructor for a child class in C#

An initializing constructor is a constructor that takes parameters to make it easy to initialize an object’s properties. This example shows how to reuse a class’s initializing constructor for a child class. The following Person class has an initializing constructor. … Continue reading

Posted in classes, OOP | Tagged , , , , , , , , , , , , , , | Leave a comment

Use the is operator in C#

The is operator lets you determine whether you can convert an object to a particular type. For example, suppose the Student class inherits from Person, and that student and person are objects from the obvious classes. Then, for example, the … Continue reading

Posted in classes, OOP, syntax | Tagged , , , , , , , , , | Leave a comment