Home

A Generic Class With Multiple Parameters

 

Using Multiple Type Parameters

As done for generic methods, when creating a generic class, you can specify more than one parameter type. To do this, in the <> operator, after the first generic type, enter a comma and another generic type. Here is an example:

public class Exercise<T, V>
{
}

If you know for sure that the parameters will be of the same type, you can use one method to process both. Otherwise, you can declare the necessary members for each type. You can also create a method that would take many arguments with each argument of a particular type. Here are examples:

public class Exercise<T, V>
{
    private T t;
    private V v;

    public void SetTValue(T value)
    {
        t = value;
    }

    public T GetTValue()
    {
        return t;
    }

    public void SetVValue(V value)
    {
        v = value;
    }
    public V GetVValue()
    {
        return v;
    }

    public  void Show(T tValue, V vValue)
    {
        Console.WriteLine(L"First:  {0}\nSecond: {1}", tValue, vValue);
    }
}

When declaring a variable for the class, make sure you appropriately specify the list of parameter types. Here are two examples:

class Program
{
    static int Main()
    {
        Exercise<int, int>; IntTypes = new Exercise<int, int>;();
        IntTypes.SetTValue(246);
        IntTypes.SetVValue(6088);
        IntTypes.Show(IntTypes.GetTValue(), IntTypes.GetVValue());

        Exercise<double, double>; DoubleTypes = new Exercise<double, double>;();
        DoubleTypes.SetTValue(355.65);
        DoubleTypes.SetVValue(1785.426);
        DoubleTypes.Show(DoubleTypes.GetTValue(), DoubleTypes.GetVValue());

        Exercise<short, decimal>; Disparate = new Exercise<short, decimal>;();
        DoubleTypes.SetTValue(42);
        DoubleTypes.SetVValue(245580.35);
        DoubleTypes.Show(DoubleTypes.GetTValue(), DoubleTypes.GetVValue());
        
        return 0;
    }
}

This would produce:

First:  246
Second: 6088

First:  355.65
Second: 1785.426

First:  42
Second: 245580.35

Press any key to continue . . .

If a generic class has more than one parameter type, they don't have to be of the same type. At the time you are creating the class, you may not specify their types but you can anticipate that they would be different. It is when you declare the variable that you would need to determine their precise types.

Even if the parameters are of primitive types, you can first declare the variables and pass them to the class.

A Class as a Parameter Type

So far, in our examples, we treated the parameter type as a primitive data type. A parameter type can also be a formal class, either one that you created yourself or one that exists as part of the C# language. When creating the generic class, you must follow all the rules we have reviewed so far for generic classess. Here is such a simple class:

public class Exercise
{
    public void Show(TypeOfValue val)
    {
        Console.WriteLine("{0}\n", val.ToString());
    }
}

As mentioned already, the class that would be processed by the generic one must have been previously created so it can be used as a parameter. When declaring a variable of the generic class, make sure you enter the name of the normal class in place of the parameter type. Everything else is as we have done so far. Here is an example:

using System;

namespace ConsoleApplication3
{
    public class FourSideGeometricFigure
    {
        private string nm;
        private double bs;
        private double hg;

        public string Name
        {
            get { return nm; }
            set { nm = value; }
        }

        public double Base
        {
            get { return bs; }
            set { bs = value; }
        }

        public double Height
        {
            get { return hg; }
            set { hg = value; }
        }

        public double Area
        {
            get { return bs * hg; }
        }

        public override string ToString()
        {
            string result = "Type:   " + nm + "\n" +
                            "Base:   " + bs.ToString() + "\n" + 
                            "Height: " + hg.ToString() + "\n" +
                            "Area:   " + Area.ToString();
            return result;
        }
    }

    public class Exercise
    {
        public void Show(TypeOfValue val)
        {
            Console.WriteLine("{0}\n", val.ToString());
        }
    }

    class Program
    {
        static int Main()
        {
            FourSideGeometricFigure sqr = new FourSideGeometricFigure();
            sqr.Name   = "Square";
            sqr.Base   = 36.82;
            sqr.Height = 36.82;
            Exercise<FourSideGeometricFigure> exoSquare = 
                new Exercise<FourSideGeometricFigure>();
            exoSquare.Show(sqr);

            Console.WriteLine();

            FourSideGeometricFigure rect = new FourSideGeometricFigure();
            rect.Name   = "Rectangle";
            rect.Base   = 52.94;
            rect.Height = 27.58;

            Exercise<FourSideGeometricFigure> exoRect = 
                new Exercise<FourSideGeometricFigure>();
            exoRect.Show(rect);
            return 0;
        }
    }
}

This would produce:

Type:   Square
Base:   36.82
Height: 36.82
Area:   1355.7124


Type:   Rectangle
Base:   52.94
Height: 27.58
Area:   1460.0852

Press any key to continue . . .

In the same way, you can create a generic class that takes more than one parameter.

 

Previous Copyright 2006 FunctionX, Inc. Next