Home

A Generic Method With Various Parameters

 

Introduction

Just like a method can take one argument, it can take various generic parameters. You can pass one argument as a known type and the other as a generic type. Here is an example:

using System;

public class Exercise
{
    public void Show<TypeOfValue>(string msg, TypeOfValue value)
    {
        Console.WriteLine("{0}: {1}", msg, value);
    }
}

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

        exo.Show<int>("Integer", 246);

        exo.Show<char>("Character", 'G');

        exo.Show<double>("Decimal", 355.65);
        
        return 0;
    }
}

This would produce:

Integer: 246
Character: G
Decimal: 355.65
Press any key to continue . . .

Although we directly passed the values to the method when calling it, you can first declare a variable before passing it to the method. Here are examples:

using System;

public class Exercise
{
    public void Show<TypeOfValue>(string msg, TypeOfValue value)
    {
        Console.WriteLine("{0}: {1}", msg, value);
    }
}

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

        string message = "Integer";
        const int iValue = 246;
        exo.Show<int>(message, iValue);

        message = "Character";
        const char cValue = 'G';
        exo.Show<char>(message, cValue);

        message = "Decimal";
        const double dValue = 355.65;
        exo.Show<double>(message, dValue);
        
        return 0;
    }
}

Practical LearningPractical Learning: Creating and Using a Method With Various Parameters

  1. Acccess the StoreItem.cs file
  2. To create and use a method with various parameters, make the following changes:
     
    namespace CommercialStore1
    {
        public class StoreItem
        {
            . . .
    
            public void ShowItem<T>(string content, int index, T item)
            {
                Console.WriteLine("{0}: {1} {2}", content, index, item);
            }
        }
    }
  3. Access the Program.cs file and change it as follows:
     
    namespace CommercialStore1
    {   
        class Program
        {
            static int Main()
            {
                . . .
    
                for (int i = 0; i < exo.Count(); i++)
                {
                    Exercise.CItem One = exo.Retrieve(i);
    
                    exo.ShowItem<double>("Item", i+1, One.Item);
                }
    
                Console.WriteLine("\nNumber of Items: {0}\n", exo.Count());
    
                return 0;
            }
        }
    }
  4. Execute the application
  5. Close the DOS window

A Generic Method With Various Parameter Types

As seen above, you can pass different arguments to a method. You can also pass different parameter types, in any appropriate order of your choice, to a method. To pass two parameter types to a method, inside its <> operator, enter the names of two parameter types separated by a comma. Here is an example:

public class Exercise
{
    public void Show<FirstType, SecondType>()
    {
    }
}

If you want to use the parameter types, you can pass an argument for each to the method. Remember that each parameter type represents a data type; so you can use it as the type of an argument. Here are examples:

public class Exercise
{
    public void Show<FirstType, SecondType>(FirstType first,
                                            SecondType second)
    {
    }
}

In the body of the method, you can then use the arguments as you see fit. For example, you can display their values by passing them to the Console.WriteLine() method. Here is an example:

public class Exercise
{
    public void Show<FirstType, SecondType>(FirstType first,
                                            SecondType second)
    {
        Console.WriteLine("First:  {0}\nSecond: {1}\n", first, second);
    }
}

Calling a Generic Method With Various Parameter Types

To call a method that takes various parameters, you can simply pass it the value of each argument. Here is an example:

using System;

public class Exercise
{
    public void Show<FirstType, SecondType>(FirstType first,
                                            SecondType second)
    {
        Console.WriteLine("First:  {0}\nSecond: {1}\n", first, second);
    }
}

public class Program
{
    static int Main()
    {
        Exercise exo = new Exercise();
        
        int iValue = 246;
        string message = "Some Message";
        exo.Show(message, iValue);

        return 0;
    }
}

This would produce:

First:  Some Message
Second: 246

Press any key to continue . . .

An alternative is to specify the type of each argument. To do this, inside the <> operator on the right side of the name of the method, enter the data types separated by a comma. Here are examples:

using System;

public class Exercise
{
    public void Show<FirstType, SecondType>(FirstType first,
                                            SecondType second)
    {
        Console.WriteLine("First:  {0}\nSecond: {1}\n", first, second);
    }
}

public class Program
{
    static int Main()
    {
        Exercise exo = new Exercise();
        
        int iValue = 246;
        string message = "Some Message";
        exo.Show(message, iValue);

        iValue = 85;
        char cValue = 'G';
        exo.Show<int, char>(iValue, cValue);

        double weeklyHours = 42.50d;
        double hourlySalary = 25.05;
        exo.Show<double, double>(weeklyHours, hourlySalary);

        return 0;
    }
}

This would produce:

First:  Some Message
Second: 246

First:  85
Second: G

First:  42.5
Second: 25.05

Press any key to continue . . .

Notice that the arguments can be of the same type or different types. It is up to you to determine the type of a particular argument when calling the method.

 

Previous Copyright © 2006 FunctionX, Inc. Next