Home

Generic Classes

 

Introduction

Like a method, a class can be created as a generic. When a class is created as generic, it is asked to process a value wihtout knowing what type that value is. This means that the class will known the type of value only when it is called.

To create a generic class, on the right side of the name of the class, type the <> operator and enter a name for the parameter type. Here is an example:

public class Exercise<TypeOfValue>
{
}

This parameter type is just a representative of a data type. As a data type, you can use it to declare a variable in the body of the class. Here is an example:

public class Exercise<TypeOfValue>
{
    public TypeOfValue value;
}

After declaring such a variable, you can use it in your application. For example, you can access it outside of the class using the period operator. Inside of the class, one way you can use the variable is to display its value using one of the methods of the class. Here is an example:

public class Exercise<TypeOfValue>
{
    public TypeOfValue value;

    public void Show()
    {
        Console.WriteLine("Value:  {0}\n", value);
    }
}

Practical LearningPractical Learning: Introducing Generic Classes

  1. To start a new program, on the main menu, click the File -> New Project ...
  2. Click Console Application. Set the Name to CommercialStore2 and press Enter
  3. To create a new class, on the main menu, click Project -> Add Class...
  4. Set the Name to ListOfItems and click OK
  5. Change the file as follows:
     
    using System;
    
    namespace CommercialStore2
    {
        class ListOfItems
        {
            public class CItem
            {
                public double Item;
                public CItem Next;
            }
    
            public ListOfItems()
            {
                Head = null;
                Size = 0;
            }
    
            public CItem Head;
            public int Size;
    
            public int Count()
            {
                return Size;
            }
    
            public int Add(CItem NewItem)
            {
                CItem Sample = new CItem();
    
                Sample = NewItem;
                Sample.Next = Head;
                Head = Sample;
    
                return Size++;
            }
    
            public CItem Retrieve(int Position)
            {
                CItem Current = Head;
    
            for (int i = Count() - 1; i > Position && Current != null; i--)
                {
                    Current = Current.Next;
                }
    
                return Current;
            }
        }
    }
  6. Access the Program.cs file and change it as follows:
     
    using System;
    
    namespace CommercialStore2
    {
        class Program
        {
            static int Main()
            {
                ListOfItems exo = new ListOfItems();
                ListOfItems.CItem Part;
    
                Part = new ListOfItems.CItem();
                Part.Item = 97.43;
                exo.Add(Part);
    
                Part = new ListOfItems.CItem();
                Part.Item = 274.87;
                exo.Add(Part);
    
                Part = new ListOfItems.CItem();
                Part.Item = 8.7873;
                exo.Add(Part);
    
                Part = new ListOfItems.CItem();
                Part.Item = 2764.4;
                exo.Add(Part);
    
                Part = new ListOfItems.CItem();
                Part.Item = 92.4662;
                exo.Add(Part);
    
                Part = new ListOfItems.CItem();
                Part.Item = 66800.85;
                exo.Add(Part);
    
                Console.WriteLine("-=- List of Items -=-");
    
                for (int i = 0; i < exo.Count(); i++)
                {
                    ListOfItems.CItem One = exo.Retrieve(i);
    
                    Console.WriteLine("Item: {0}", One.Item);
                }
    
                Console.WriteLine("\nNumber of Items: {0}\n", exo.Count());
    	    return 0;
            }
        }
    }
  7. Exercute the application and test it:
     
    -=- List of Items -=-
    Item: 97.43
    Item: 274.87
    Item: 8.7873
    Item: 2764.4
    Item: 92.4662
    Item: 66800.85
    
    Number of Items: 6
    
    Press any key to continue . . .
  8. Close the DOS window

Using a Generic Class

After creating a generic class, you can use it. One way to do this, as we have learned in previous lessons, consists of declaring a variable for it. In previous lessons, to declare a variable of a class, we would write:

Exercise exo = new Exercise();

If the class is generic, on the right side, type the <> operator. Inside of this operator, enter the data type that will be processed as the parameter type of the generic class. Here is an example:

using System;

public class Exercise<TypeOfValue>
{
    public TypeOfValue value;

    public void Show()
    {
        Console.WriteLine("Value:  {0}\n", value);
    }
}

public class Program
{
    static int Main()
    {
        Exercise<int> exo = new Exercise<int>();

        return 0;
    }
}

After declaring the variable, you can then access the member(s) of the class using the period operator. Here are examples:

using System;

public class Exercise<TypeOfValue>
{
    public TypeOfValue value;

    public void Show()
    {
        Console.WriteLine("Value:  {0}\n", value);
    }
}

public class Program
{
    static int Main()
    {
        Exercise<int> exo = new Exercise<int>();
        
        int iValue = 246;
        exo.value = iValue;
        exo.Show();

        return 0;
    }
}

Passing a Parameter Type to a Method

We saw that you could declare a variable of a parameter type in the generic class. Another way you can use it is to pass it as an argument to a method and make the argument a parameter type. As seen previously, you can use the argument as you see fit. For example, you can display its value to the console. Here is an example:

using System;

public class Exercise<TypeOfValue>
{
    public void Show(TypeOfValue value)
    {
        Console.WriteLine("Value:  {0}\n", value);
    }
}

public class Program
{
    static int Main()
    {
        Exercise<int> exo = new Exercise<int>();
        
        int iValue = 246;
        exo.Show(iValue);

        return 0;
    }
}

In the same way, you can pass the parameter type to a constructor of the class. Here is an example:

public class Exercise<TypeOfValue>
{
    private TypeOfValue val;

    public Exercise(TypeOfValue v)
    {
        val = v;
    }
}

Returning a Parameter Type

Besides, or as opposed to, passing a parameter type, you can create a method that returns a parameter type. Once again, you can primarily observe the rules we reviewed for returning a value from a method. Here is an example:

using System;

public class Exercise<TypeOfValue>
{
    private TypeOfValue val;

    public Exercise(TypeOfValue v)
    {
        val = v;
    }

    public TypeOfValue GetValue()
    {
        return val;
    }
}

public class Program
{
    static int Main()
    {
        Exercise<double> exo = new Exercise<double>(35.65);

        Console.WriteLine("Value:  {0}\n", exo.GetValue());

        return 0;
    }
}

Practical LearningPractical Learning: Returning a Parameter Type

  1. Open the ListOfItems.cs source file
  2. To apply what we have reviewed, change the file as follows:
     
    using System;
    
    namespace CommercialStore2
    {
        class ListOfItems
        {
            public class CItem<T>
            {
                public double Item;
                public CItem<T> Next;
            }
    
            public ListOfItems()
            {
                Head = null;
                Size = 0;
            }
    
            public CItem<double> Head;
            public int Size;
    
            public int Count()
            {
                return Size;
            }
    
            public int Add(CItem<double> NewItem)
            {
                CItem<double> Sample = new CItem<double>();
    
                Sample = NewItem;
                Sample.Next = Head;
                Head = Sample;
    
                return Size++;
            }
    
            public CItem<double> Retrieve(int Position)
            {
                CItem<double> Current = Head;
    
                for (int i = Count() - 1; i > Position && Current != null; i--)
                {
                    Current = Current.Next;
                }
    
                return Current;
            }
        }
    }
  3. Access the Program file and change it as follows:
     
    using System;
    
    namespace CommercialStore2
    {
        class Program
        {
            static int Main()
            {
                ListOfItems exo = new ListOfItems();
                ListOfItems.CItem<double> Part;
    
                Part = new ListOfItems.CItem<double>();
                Part.Item = 97.43;
                exo.Add(Part);
    
                Part = new ListOfItems.CItem<double>();
                Part.Item = 274.87;
                exo.Add(Part);
    
                Part = new ListOfItems.CItem<double>();
                Part.Item = 8.7873;
                exo.Add(Part);
    
                Part = new ListOfItems.CItem<double>();
                Part.Item = 2764.4;
                exo.Add(Part);
    
                Part = new ListOfItems.CItem<double>();
                Part.Item = 92.4662;
                exo.Add(Part);
    
                Part = new ListOfItems.CItem<double>();
                Part.Item = 66800.85;
                exo.Add(Part);
    
                Console.WriteLine("-=- List of Items -=-");
    
                for (int i = 0; i < exo.Count(); i++)
                {
                    ListOfItems.CItem<double> One = exo.Retrieve(i);
    
                    Console.WriteLine("Item: {0}", One.Item);
                }
    
                Console.WriteLine("\nNumber of Items: {0}\n", exo.Count());
                return 0;
            }
        }
    }
  4. Execute the application and test it 
  5. Close the DOS window

A Property of the Parameter Type

You can create a property that is of the parameter type of the generic class. There is no significant rule to follow when creating the property, except that you should remember that, at the time you are creating the property, the class doesn't know the type of the parameter. Here is an example:

using System;

public class Exercise<TypeOfValue>
{
    private TypeOfValue val;

    public TypeOfValue Value
    {
        get { return val; }
        set { val = value; }
    }

    public void Show()
    {
        Console.WriteLine("Value:  {0}\n", val);
    }
}

public class Program
{
    static int Main()
    {
        Exercise<int> exo = new Exercise<int>();
        
        exo.Value = 246;
        exo.Show();

        return 0;
    }
}
 

Previous Copyright © 2006-2016, FunctionX, Inc. Next