Like the Generic class, there can be generic methods, and a generic method does not necessarily have to be inside a generic class. A generic method can be inside a non-generic class as well. To create a generic method, you have to place the type parameter next to the method name and before the parenthesis. The general form is given here:
access-modifier return-type method-name<type-parameter>(params){ method-body }
Now, let's look at an example of a generic method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Chapter7
{
class Hello
{
public static T Larger<T>(T a, T b) where T : IComparable<T>
{
return a.CompareTo(b) > 0 ? a : b;
}
}
class Code_7_4
{
static void Main(string[] args)
{
int result = Hello.Larger<int>(3, 4);
...