Base Classes for Implementing Abstractions

Base classes for implementing abstractions are classes designed to assist developers in implementing abstract classes and interfaces (abstractions). They provide some of the implementation details for an abstraction and in some cases they may be useable without inheritance. For example, Collection<T> can be used to create a collection or can be inherited from to define a strongly-typed collection class.

The following code example demonstrates using the Collection<T> class to create a strongly-typed collection object.

Public Class PointManager
    Implements IEnumerable

    Private pointCollection As Collection(Of Point) = New Collection(Of Point)

    Public Sub AddPoint(ByVal p As Point)
        pointCollection.Add(p)
    End Sub

    Public Function RemovePoint(ByVal p As Point) As Boolean
        Return pointCollection.Remove(p)
    End Function

    Public Function GetEnumerator() As IEnumerator _
        Implements IEnumerable.GetEnumerator

        Return pointCollection.GetEnumerator
    End Function
End Class
public class PointManager : IEnumerable
{
    Collection<Point> pointCollection = new Collection<Point>();

    public void AddPoint(Point p)
    {
        pointCollection.Add(p);
    }
    public bool RemovePoint(Point p)
    {
        return pointCollection.Remove(p);
    }
    public IEnumerator GetEnumerator()
    {
        return pointCollection.GetEnumerator();
    }
}

The following code example demonstrates using the Collection<T> class to define a strongly-typed collection.

Public Class PointCollection
    Inherits Collection(Of Point)
End Class
public class PointCollection : Collection<Point> {}

The CollectionBase class is another example of a .NET Framework base class. This class helps developers implement non-generic collections. Unlike Collection<T>, CollectionBase cannot be used directly.

Base classes for implementing abstractions should be provided as part of a library only when they add significant value to developers who use the library. In the case where a base class helps only in implementing a library, the base class should not be publicly visible. To use a base class internally to simplify library development, public members should delegate work to the base class instead of inheriting from it.

Consider making base classes abstract even if they do not contain any abstract members. This clearly communicates to the users that the class is designed solely to be inherited from.

Consider placing base classes in a separate namespace from the main scenario APIs. By definition, base classes are intended for advanced extensibility scenarios and are not interesting to the majority of users.

Avoid naming bases class with the Base suffix if the class is intended to be used in public APIs.

If the library exposes the base class as a return type or parameter type, it should not have the Base suffix.

Portions Copyright 2005 Microsoft Corporation. All rights reserved.

Portions Copyright Addison-Wesley Corporation. All rights reserved.

For more information on design guidelines, see the "Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries" book by Krzysztof Cwalina and Brad Abrams, published by Addison-Wesley, 2005.

See Also

Other Resources

Design Guidelines for Developing Class Libraries

Designing for Extensibility