Unity study notes: generics

Pan type it is the type to do parameter

 

Pan- type constraints

T:struct

The type parameter must be a value type . You can specify  any value type except Nullable . For more information, see use can (C # Programming Guide) is a type of null .

T:class

The type parameter must be a reference type ; this also applies to any class, interface, delegate, or array type.

T:new()

Type parameters must have a public constructor with no parameters . When used with other constraints, the new()  constraint must be specified last.

T: <base class name>

The type parameter must be the specified base class or be derived from the specified base class .

T: <interface name>

The type parameter must be a specified interface or implement a specified interface . You can specify multiple interface constraints. Constraint interfaces can also be generic.

T:U

The type parameter provided for T must be a parameter provided for U or derived from a parameter provided for U. This is called a bare type constraint .

Generics should be used

Generic class, generic method, generic interface, generic delegate, generic collection

In a word, generics can be used where data types are needed

Commonly used generic design some general (based on different types of multiplexing) algorithms.

Practice case↓ ab swap

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Collections;

namespace DemoGenric
{
    class GenricBase1
    {
        static void Main2(string[] args)
        {
            My<int> myobj = new My<int>();
            myobj.Add(1);    
            //myobj.Add("aa");//更安全,第一时间告诉 放的不合适
            myobj.Add(2);     //不需要类型转换 性能更好; object<int 很多 性能
            myobj.Add(3);
            myobj.Add(4);
        }
        static void Main1(string[] args)
        {
            My<int> myobj = new My<int>();
            myobj.Add(1);

            My<String> myobj2 = new My<String>();
            myobj2.Add("aa");

            Your your1 = new Your();
            //your1.Add<int>(1);
            //your1.Add<int>(2);
            your1.Add(1);
            your1.Add(2);

        }

        static void Main3(string[] args)
        {
            string a = "aas";
            string b = "bb";
            Console.WriteLine(a);
            Console.WriteLine(b);
            My<string> my = new My<string>();
            my.Swap(ref a, ref b);
            //Fun????
            Console.WriteLine(a);
            Console.WriteLine(b);

            Console.ReadKey();
        }
    }

    //class My
    //{
    //    ArrayList arrList = new ArrayList();//
    //    public void Add(int i)
    //    {
    //        arrList.Add(i);
    //    }
    //}
    class My<T>
    {
        ArrayList arrList = new ArrayList();//
        public void Add(T i)
        {
            arrList.Add(i);
        }
        public void Swap(ref T a, ref T b)
        {
            T temp = a;
            a = b;
            b = temp;
        }
    }
    class Your
    {
        ArrayList arrList = new ArrayList();//
        public void Add<T>(T i)
        {
            arrList.Add(i);
        }
        public void Swap<T>(ref T a, ref T b)
        {
            T temp = a;
            a = b;
            b = temp;
        }
    }
}

 

Generic constraint where

namespace DemoGenric2
{
    class GenricBase2
    {
        static void Main11()
        {
            //My<int> my1 = new My<int>();
            //My<NormalizationForm> my11 = new My<NormalizationForm>();

            //My<string> my2 = new My<string>();
            My<Dog> my3 = new My<Dog>();
        
        }
    }
    //class My<T> where T:struct //值类型:枚举 结构
    //{ 
    
    //}
    //class My<T> where T : class //引用类型:类,接口,委托
    //{

    //}
    class My<T> where T :Animal,IFly
    {

    }
    interface IFly
    { }
    class Animal
    { }
    class Dog:Animal,IFly { }
}

 

Guess you like

Origin blog.csdn.net/huanyu0127/article/details/107648635