Wrox Home  
Professional .NET 2.0 Generics
by Tod Golding
October 2005, Paperback

Protected Members

Any discussion of inheritance would be incomplete without also examining the accessibility of protected members. First, I should make it clear that all the rules that govern access to protected members are unchanged for closed types. Things get a bit more interesting when you look at protected members that appear within a generic class. These members are accessible too. In fact, they are accessible in some ways you might not expect.

Here's an example that declares a base class with a protected, generic field that will then be referenced in a descendant class:

 [VB code]
Public Class MyBaseClass(Of T)
    Protected _var1 As T

    Public Sub New(ByVal var1 As T)
        Me.var1 = var1
    End Sub
End Class

Public Class MySubClass(Of T, U)
    Inherits MyBaseClass(Of T)
    Private _var2 As U

    Public Sub New(ByVal var1 As T, ByVal var2 As U)
        Me._var2 = var2
    End Sub

    Private Sub Foo1()
        Dim localVar as T = Me._var1
    End Sub

    Private Sub Foo2()
        Dim sub1 As New MySubClass(Of Integer, String)(1, "12")
        Dim val1 As Integer = sub1._var1
        Dim sub2 As New MySubClass(Of Double, Double)(1.0, 5.8)
        Dim val2 As Double = sub2._var1
        End Sub
End Class 
[C# code]
using System;

public class MyBaseClass<T> {
    protected T _var1;

    public MyBaseClass(T var1) {
        this.var1 = _var1;

public class MySubClass<T, U> : MyBaseClass<T> {
    private U _var2;

    public MySubClass(T var1, U var2) : base(var1) {
        this._var2 = var2;

    private void Foo1() {
        localVar T = this._var1;

    private void Foo2() {
        MySubClass<int, String> sub1 
			= new MySubClass<int, String>(1, "12");
        int val1 = sub1._var1;
        MySubClass<Double, Double> sub2 
			= new MySubClass<Double, Double>(1.0, 5.8);
        Double val2 = sub2._var1;

This example actually ends up illustrating two key points. It includes a protected field in the base class, var1, that is then accessed by the subclass. The method Foo1() accesses var1 successfully, much like it would any other inherited, protected data member. The method Foo2() is the more interesting example. It constructs two separate instances of MySubClass and, because we're in the scope of the descendant class, is able to access the protected member var1 via these constructed types.

This little foray into generic classes and some of their characteristics should give you a better idea for how generics can be applied to bring a new dimension to your data types. While generic classes certainly share much in common with their non-generic counterpart, you'll also discover a number of subtle differences that you'll have to keep in mind as start creating your own generic types.

This article is adapted from Professional .NET 2.0 Generics by (Wrox, 2005, ISBN: 0-7645-5988-5), from chapter 4 "Generic Classes." Tod Golding has 20 years of experience as a software developer, lead architect, and development manager for organizations engaged in the delivery of large-scale commercial and internal solutions. He has an extensive background leveraging .NET, J2EE, and Windows DNA technologies, and is skilled with C#, Java, and C++.