Csharp/CSharp Tutorial/Generic/Generic Method

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

A generic method

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

A generic method: constrol the parameter

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.

constrained method calls

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));
    }
}
10

Generic and non Generic compare

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 );
        }
    }
}

Generic Method Demo

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);
            }
        }
    }

Generic Method Reflection

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);
        }
    }

Multiple Argument Inference with value and object

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());
        }
    }