Generics: Generic Methods |
|
Introduction |
As is usually done, imagine you want to pass different types of values to various methods of a class to primarily accomplish the same purpose. You may be tempted to overloaded a method in various versions as follows: using System; public class Exercise { // Display the value of an integer public void Show(int value) { Console.WriteLine(value); } // Display the value of a double-precesion value public void Show(double value) { Console.WriteLine(value); } // Display the value of a character public void Show(char value) { Console.WriteLine(value); } } |
public class Program { static int Main() { Exercise exo = new Exercise(); // Call the version of the method that displays an integer exo.Show(246); // Call the version of the method that displays a character exo.Show('G'); // Call the version of the method that displays a decimal exo.Show(355.65); return 0; } } This would produce: 246 G 355.65 Press any key to continue . . . We passed a constant value directly to the method when we called it. Remember that you can also first declare a variable, assign it a value, and then pass that variable to the method. Here are examples: public class Program { static int Main() { Exercise exo = new Exercise(); // Call the version of the method that displays an integer int Value1 = 246; exo.Show(Value1); // Call the version of the method that displays a character char Value2 = 'G'; exo.Show(Value2); // Call the version of the method that displays a decimal double Value3 = 355.65; exo.Show(Value3); return 0; } } Although this is based on the concept of method overloading, another way you can solve this type of problem is to create one method that doesn't know the type of value that would be passed to it but the method is equipped to process the value appropriately. Based on the above program, you can create one method that takes an argument and it displays its value. To do this, at the time you are defining the method, you only let it know that it would receive an argument but you don't specify the type of value that it will process. Such a method is referred to as generic.
A generic method is a method that can process a value whose type is known only when the variable is accessed. To create a generic method, on the right side of the name of the method, type the <> operator. Inside of this operator, enter a letter or a name, which is referred to as parameter type. Here is an example: public class Exercise { public void Show<TypeOfValue>() { } } One of the ways you can use the parameter type is to pass an argument to the method. You do this by preceding the name of the argument with the parameter type. Here is an example: public class Exercise { public void Show<TypeOfValue>(TypeOfValue value) { } } In the body of the method, you can process the argument as you see fit. At a minimum, and based on our earlier program, you can simply display the value by passing it to the Console.WriteLine() method. Here is an example: public class Exercise { public void Show<TypeOfValue>(TypeOfValue value) { Console.WriteLine(value); } } By tradition, most programmers and most documents use the letter T for the parameter type.
As mentioned earlier, one of the particularities of a generic method is that, at the time it is defined, the method doesn't know the type of the parameter. This means that, when calling the method, you must make sure you clearly specify the type of value that will be processed. You can do this by directly passing (a constant of) the type of value that the method will process. Here are different examples of calling our Show() method: using System; public class Exercise { public void Show<TypeOfValue>(TypeOfValue value) { Console.WriteLine(value); } } public class Program { static int Main() { Exercise exo = new Exercise(); // Call the version of the function that displays an integer int Value1 = 246; exo.Show(Value1); // Call the version of the function that displays a character char Value2 = 'G'; exo.Show(Value2); // Call the version of the function that displays a decimal double Value3 = 355.65; exo.Show(Value3); return 0; } } When complied and executed, this program would produce: 246 G 355.65 Press any key to continue . . . As an alternative, you can type the name of the method, followed by angle brackets. Inside of the brackets, enter the data type of the value that will be processed. After the angle brackets, open the parentheses and, inside of them, type the constant value that will be processed. Here are examples: using System; public class Exercise { public void Show<TypeOfValue>(TypeOfValue value) { Console.WriteLine(value); } } public class Program { static int Main() { Exercise exo = new Exercise(); // Call the version of the function that displays an integer int Value1 = 246; exo.Show<int>(Value1); // Call the version of the function that displays a character char Value2 = 'G'; exo.Show<char>(Value2); // Call the version of the function that displays a decimal double Value3 = 355.65; exo.Show<double>(Value3); return 0; } } You can also declare the value as a constant before passing it to the method.
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|