Csharp/CSharp Tutorial/Generic/Generic Method
Содержание
A generic method
<source lang="csharp">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); }
}</source>
True True
A generic method: constrol the parameter
<source lang="csharp">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); }
}</source>
1 2 3 Generics are powerful.
constrained method calls
<source lang="csharp">using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.rupilerServices; public class MainClass {
static string GetToString<T>(T input) { return input.ToString(); } public static void Main() { Console.WriteLine(GetToString<int>(10)); }
}</source>
10
Generic and non Generic compare
<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; public class MainClass {
public void NonGeneric( Stack stack ) { foreach( object o in stack ) { int number = (int) o; Console.WriteLine( number ); } } public void Generic( Stack<int> stack ) { foreach( int number in stack ) { Console.WriteLine( number ); } }
}</source>
Generic Method Demo
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel;
class GenericMethodDemo { static List<T> MakeList<T> (T first, T second) { List<T> list = new List<T>(); list.Add (first); list.Add (second); return list; } static void Main() { List<string> list = MakeList<string>("Line 1", "Line 2"); foreach (string x in list) { Console.WriteLine(x); } } }</source>
Generic Method Reflection
<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.ruponentModel; using System.Reflection;
class GenericMethodReflection { public static void PrintTypeParameter<T>() { Console.WriteLine (typeof(T)); } static void Main() { Type type = typeof(GenericMethodReflection); MethodInfo definition = type.GetMethod("PrintTypeParameter"); MethodInfo constructed; constructed = definition.MakeGenericMethod(typeof(string)); constructed.Invoke(null, null); } }</source>
Multiple Argument Inference with value and object
<source lang="csharp">using System; using System.ruponentModel;
class MainClass { static void PrintType<T>(T first, T second) { Console.WriteLine(typeof(T)); } static void Main() { PrintType(1, new object()); } }</source>