A generic method: constrol the parameter : Generic Method « Generic « C# / CSharp Tutorial






using System; 
 
class ArrayUtils {  
 
  public static void print<T>(T e, T[] src) { 
    for(int i= 0;i<src.Length;i++){
      Console.WriteLine(src[i]);
    }
  } 
} 
 
class MainClass { 
  public static void Main() { 
    int[] nums = { 1, 2, 3 }; 
 
    ArrayUtils.print(99, nums); 
 
    Console.WriteLine(); 
 
 
    string[] strs = { "Generics", "are", "powerful."}; 
 
    ArrayUtils.print("in C#", strs); 
 
 
    // invalid 
    // ArrayUtils.copyInsert(0.01, nums); 
 
  } 
}
1
2
3

Generics
are
powerful.








18.12.Generic Method
18.12.1.A generic method
18.12.2.constrained method calls
18.12.3.A generic method: constrol the parameter
18.12.4.Generic and non Generic compare
18.12.5.Generic Method Demo
18.12.6.Generic Method Reflection
18.12.7.Multiple Argument Inference with value and object