泛型

泛型方法

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

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            var Result = GetT<int, int>(30, 40);
            Console.WriteLine(Result);

            Console.ReadLine();
        }
        static T GetT<T, T1>(T a, T1 b)
        {
            //dynamic表示在运行时解析对象
            //这里不可以直接返回a+b,必须使用dynamic
            dynamic a1 = a;
            dynamic b1 = b;
            return a1 + b1;
        }
    }
}
View Code

default

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

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            var Result = GetT<int, int>(10);
            Console.WriteLine(Result);

            Console.ReadLine();
        }
        static T GetT<T, T1>(T a)
        {
            T Parma1;
            T1 Parma2;
            Parma1 = a;
            //泛型变量不可以等于null,也不能New
            //default关键字:如果T1是引用类型,则赋值null,如果是值类型就给值类型默认值
            Parma2 = default(T1);
            
            return Parma1;
        }
    }
}
View Code

泛型类

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

namespace Generic
{
   public class GetGeneric<T1,T2>
    {
        public T1 Param { get; set; }
        public T1 GetT(T1 Param1)
        {
            this.Param = Param1;
            return this.Param;
        }

    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            GetGeneric<int, string> getGeneric = new GetGeneric<int, string>();
            var Result = getGeneric.GetT(20);
            Console.WriteLine(Result);
            Console.ReadLine();
        }
    }
}
View Code

泛型约束

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

namespace Generic
{
    public class GetGeneric<T1, T2, T3>
         where T1 : struct//指定T1必须是值类型
         where T2 : class//指定T2必须是引用类型
         where T3:new()//指定T3这个类型中,必须有一个无参数的构造方法,这个约束必须放到最后
    {
        public T1 Param { get; set; }
        public T1 GetT(T1 Param1)
        {
            this.Param = Param1;
            return this.Param;
        }

    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            GetGeneric<int, string,Class1> getGeneric = new GetGeneric<int, string,Class1>();
            var Result = getGeneric.GetT(20);
            Console.WriteLine(Result);
            Console.ReadLine();
        }
    }
}
View Code

 泛型约束中的New使用

 

 

原文地址:https://www.cnblogs.com/Luck1996/p/12010699.html