Generics
Generic Method Creation
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 Generator
{
public void Show
{
}
}
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 Generator
{
public void Show
{
}
}
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 Generator
{
public void Show
{
Console.WriteLine(value);
}
}
By tradition, most programmers and most documents use the letter T for the parameter type.
Creating a Generic Method
To create a generic method, change the ShowItem() method as follows:
public void ShowItem
{
Console.WriteLine("Item: {0}", item);
}
Calling a Generic Method
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 Generator
{
public void Show
{
Console.WriteLine(value);
}
}
public class Exercise
{
static int Main()
{
var exo = new Generator();
// Call the version of the function that displays an integer
var Value1 = 246;
exo.Show(Value1);
// Call the version of the function that displays a character
var Value2 = 'G';
exo.Show(Value2);
// Call the version of the function that displays a decimal
var 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 . . .
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 Generator
{
public void Show
{
}
}
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 Generator
{
public void Show
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 Generator
{
public void Show
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 Generator
{
public void Show
SecondType second)
{
Console.WriteLine("First: {0}\nSecond: {1}\n", first, second);
}
}
public class Exercise
{
static int Main()
{
var exo = new Generator();
var iValue = 246;
var 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 Generator
{
public void Show
SecondType second)
{
Console.WriteLine("First: {0}\nSecond: {1}\n", first, second);
}
}
public class Exercise
{
static int Main()
{
var exo = new Generator();
var iValue = 246;
var message = "Some Message";
exo.Show(message, iValue);
iValue = 85;
var cValue = 'G';
exo.Show
var weeklyHours = 42.50d;
var hourlySalary = 25.05;
exo.Show
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.
No comments:
Post a Comment