[Unity | C #] Basics (10) - Generic (Generic) / generic constraints (where)

[Learning materials]

        > Documents Online

            The official document: https://docs.microsoft.com/zh-cn/dotnet/csharp/

            Rookie Tutorial (Advanced Tutorial) : https://www.runoob.com/csharp/csharp-tutorial.html

        > Video Tutorial

            Tencent college, Siki College

        > Books 

    "C # graphic tutorials" (Chapter 17) : https://www.cnblogs.com/moonache/p/7687551.html

 

【Learning Content】 

  > Rookie Tutorial: Advanced Tutorial section (generic)

  > "C # graphic tutorials" (Chapter 17)

 


【notes】

  • definition
    • And in C ++ template template <T> as
    • Generic type can be used: class, structure, interfaces, delegates, the method
    • NOTE: Generic not only a T, can define multiple generic T1, T2, T3 ...

 

  •  Generic class (class)
    • Unknown type T , when you create an object determined to avoid overloading more MyList
    • // type T is determined at object creation
       public  class of MyList <T> 
      { 
          Private  int size = 10 ;
           public T [] values;
           public of MyList () 
          { 
              values = new new T [size]; 
          } 
      } 
      
      void the Start () 
      { 
          of MyList < int > = myList_int new new of MyList < int > ();           // to T int 
          of MyList < String > = myList_string new new of MyList < String>(); // T 为 string
          myList_int.values[0] = 1;
          myList_string.values[0] = "Alice";
      }
  • Generic function
    • Reduction function overloading, a generic T all the different types of adaptation parameters
    • // 交换a和b
      public void SwapValue<T>(ref T a, ref T b)
      {
          T temp = a;
          a = b;
          b = temp;
      }
      
      void Start()
      {
          int i1 = 1, i2 = 2;
          string s1 = "Alice", s2 = "Bob";
      
          SwapValue(ref i1, ref i2); // T 为 int
          SwapValue ( REF S1, REF S2); // T is a String 
          Debug.Log (I1); // Output: 2 
          Debug.Log (S1); // Output: Bob 
      }
  • Generic delegate
    • Similar to the generic class definition
    • // 泛型委托
      delegate T NumberChanger<T>(T a, T b);
      
      public int AddNum(int a, int b)
      {
          return a + b;
      }
      public string AddString(string a, string b)
      {
          return a + b;
      }
      
      void Start()
      {
          // T 为 int
          NumberChanger<int> numberChanger_int = new NumberChanger<int>(AddNum);
          // T 为 string
          NumberChanger<string> numberChanger_string = new NumberChanger<string>(AddString);
      } 

 

  • Multi-defined generic T
    • Generic necessarily only a T, can also define a plurality, T1, T2, T3 ...
    • // multiple T generic delegate 
      the delegate T3 NumberChanger <Tl, T2, T3> (Tl A, T2 B); 
      
      // int and float addition, return String 
      public  String AddNumToString ( int A, float B) 
      { 
          return (A + B) .ToString (); 
      } 
      
      void the Start () 
      { 
          // create a generic variable 
          NumberChanger < int , a float , String > = numberChanger new new NumberChanger < int , a float , String > (AddNumToString); 
      }

 

  • Generic constraint ( WHERE )
    • T is of the generic type constraint, in addition to the definition of generic code behind   WHERE T: xxx  , xxx represents the type T must

    • For example:
    • public class Major // 职业
      {
      
      };
      public class Teacher : Major // 教师
      {
      
      }
      
      public class Person<T1, T2, T3> 
          where T1 : class    // T1 必须是个类 类型
          where T2 : struct   // T2 必须是个结构 类型
          where T3 : Major    // T3 必须是基类为Major的类 
      {
          T1 value1;
          T2 value2;
          T3 value3;
      }
      
      void Start()
      {
          // 报错:T1和T3不满足约束条件
          //Person<int, int, int> person1 = new Person<int, int, int>();
      
          // 正确:
          //  string  类型为 class System.String
          //  int     类型为 struct System.Int32
          //  Teacher 类型为 class Major
          Person<string, int, Teacher> person2 = new Person<string, int, Teacher>(); 
      }

       

 

Guess you like

Origin www.cnblogs.com/shahdza/p/12244245.html