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 -> ...
  2. Type the following:
     
    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;
            }
        }
    }
  3. To create a new file, on the main menu of Notepad, click File Save
  4. When asked whether you want to save the file, click Yes
  5. Save the file as ListOfItems.cs in a new folder named CommercialStore2
  6. In the empty file, type:
     
    using System;
    
    namespace CommercialStore2
    {
        class Program
        {
            static int Main(string[] args)
            {
                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. On the main menu, click File -> Save
  8. Save the file as Exercise.cs
  9. Get to the Command Prompt
  10. Compile the application and execute it:
     
    
            
  11. Complete the

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: Introducing Generic Classes

  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 T 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. Save the file and open the Exercise.cs source file
  4. Change it as follows:
     
    using System;
    
    namespace CommercialStore2
    {
        class Program
        {
            static int Main()
            {
                ListOfItems exo = new ListOfItems();
                ListOfItems.CItem 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;
            }
        }
    }
  5. Save the file and get to the Command Prompt
  6. Compile the application and execute it:
     
    
            
  7. Complete the

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 FunctionX, Inc. Next