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






using System; 
 
class ArrayUtils {  
 
  public static bool isBigger<T>(T[] src, T[] target) { 
    // See if target array is big enough. 
    if(target.Length < src.Length+1)  
      return false; 
    return true; 
  } 
} 
 
class GenMethDemo { 
  public static void Main() { 
    int[] nums = { 1, 2, 3 }; 
    int[] nums2 = new int[4]; 
 

    // Operate on an int array. 
    bool b = ArrayUtils.isBigger(nums, nums2); 
 
    Console.WriteLine(b); 
 
    string[] strs = { "Generics", "are", "powerful."}; 
    string[] strs2 = new string[4]; 
 
    b = ArrayUtils.isBigger(strs, strs2); 
 
    
    Console.WriteLine(b); 
 
  } 
}
True
True








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