1. 程式人生 > >C#泛型約束與呼叫

C#泛型約束與呼叫

1、C#泛型

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Student<string> stu = new Student<string>();
            stu.Age = 12;
            stu.Name = "Luck";

            string name = "小王";
            Console.WriteLine(stu.Info(name));

            //泛型類約束結構約束,T必須是值型別
            StudentStruct<structA> stustru = new StudentStruct<structA>();
            structA s;
            s.age = 24;
            Console.WriteLine(stustru.Ages(s.age));

            //泛型類約束類約束,T必須是引用型別(這裡用string)
            StudentClass<string> stuClass = new StudentClass<string>();
            name = "王力巨集";
            stuClass.className = "哥倫比亞大學";
            Console.WriteLine(stuClass.Study(name));
            Console.WriteLine("王力巨集畢業於:{0},{1};", stuClass.className, stuClass.Study(name));

            //泛型類約束類約束,T必須是引用型別(這裡用類型別)
            StudentClass2<StudentsInfo> stuInfoClass = new StudentClass2<StudentsInfo>(); StudentsInfo sclaa = new StudentsInfo();
            stuInfoClass.Study(sclaa);

            //泛型類約束介面約束,T必須是實現介面
            StudentInterface<IStudents> stuInter = new StudentInterface<IStudents>();
            int id = 5;
            DataTable dt = stuInter.dataInfo(id);
            Console.WriteLine("{0}     {1}\r\n", "ID", "Name");
            foreach (DataRow dr in dt.Rows)
            {
                Console.WriteLine("{0}     {1}\r\n", dr["ID"].ToString(), dr["Name"].ToString());
            }

            //泛型類約束,T必須是派生自基類
            StudentObj<StudentsSubclass> stuobj = new StudentObj<StudentsSubclass>();
            StudentsSubclass sub = new StudentsSubclass();
            stuobj.Studys(sub);

            //泛型類約束,建構函式約束(T必須有一個預設的建構函式)
            StudentNew<StudentConstructor> snews = new StudentNew<StudentConstructor>();
            StudentConstructor scon = new StudentConstructor();
            snews.Studys(scon);

            //泛型類約束,這裡T派生自泛型型別K,該型別稱為裸型別約束
            StudentT<StudentK<StudentsInfo>> sTK = new StudentT<StudentK<StudentsInfo>>();
            StudentK<StudentsInfo> sks = new StudentK<StudentsInfo>();
            name = "$C#語法";
            sTK.Studys(name);
        }
    }
    //基本型別
    public class StudentsInfo
    {
        public string Name { get; set; }

        public void Speak(StudentsInfo intos)
        {
            intos.Name = "溫家波";
            Console.WriteLine(Name + "/習近平召開十九大會議");
        }
    }
    //泛型類
    public class Student<T>
    {
        public int Age { get; set; }
        public string Name { get; set; }

        public string Info(T name)
        {
            return name + "是三好學生";
        }
    }

    //泛型類約束結構約束,T必須是值型別
    //byte,short,int,long,float,double,decimal,char,bool 和 struct 統稱為值型別。
    //string 和 class統稱為引用型別。
    //引用型別:基類為Objcet
    //值型別:均隱式派生自System.ValueType:
    public class StudentStruct<T> where T : struct
    {
        public string Infos(T name)
        {
            return name + "在學習";
        }

        public string Ages(int age)
        {
            return "李四的年齡是:" + age;
        }
    }

    //結構
    //這裡注意C#結構與類的區別
    public struct structA
    {
        //public int A = 90; //錯誤:“structA.A”: 結構中不能有例項欄位初始值
        public int age;
        public structA(int n)
        {
            this.age = n;
        }
    }

    //泛型類約束類約束,T必須是引用型別
    public class StudentClass<T> where T : class
    {
        public string className { get; set; }
        public string Study(T name)
        {
            return name + "在學習聲樂";
        }
    }

    public class StudentClass2<T> where T : class
    {
        public void Study(T name)
        {
            StudentsInfo stu = new StudentsInfo();
            stu.Name = "張德江";
            stu.Speak(stu);
        }
    }

    //泛型類約束介面約束,T必須是實現介面
    public class StudentInterface<T> where T : IStudents
    {
        public DataTable dataInfo(int id)
        {
            //DataTable建立行和列http://www.cnblogs.com/jRoger/articles/1887581.html
            DataTable dt = new DataTable();
            DataColumn dc = null;
            dc = dt.Columns.Add("ID", Type.GetType("System.Int32"));
            dc.AutoIncrement = true;//自動增加 
            dc.AutoIncrementSeed = 1;//起始為1 
            dc.AutoIncrementStep = 1;//步長為1 
            dc.AllowDBNull = false;
            dc = dt.Columns.Add("Age", typeof(string));
            dc = dt.Columns.Add("Name", Type.GetType("System.String"));

            DataRow newRow;
            newRow = dt.NewRow();
            newRow["Age"] = "23";
            newRow["Name"] = "湖北";

            dt.Rows.Add(newRow);
            newRow = dt.NewRow();
            newRow["Age"] = "19";
            newRow["Name"] = "五五";

            dt.Rows.Add(newRow);

            return dt;
        }
    }
    //介面
    public interface IStudents
    {
        DataTable dataInfo(int id);
    }

    //泛型類約束,T必須是派生自基類
    public class StudentObj<T> where T : StudentsSubclass
    {
        public void Studys(T name)
        {
            StudentsSubclass s = new StudentsSubclass();
            s.Name = "Net";
            s.Study(s.Name);
        }
    }

    //C#子類
    public class StudentsSubclass : StudentsInfo
    {
        public void Study(string name)
        {
            Console.WriteLine(name + "複習C#");
        }
    }

    //泛型類約束,建構函式約束(T必須有一個預設的建構函式)
    public class StudentNew<T> where T : new()
    {
        public void Studys(T name)
        {
            StudentConstructor s = new StudentConstructor();
            string Name = "new()";
            s.Study(Name);
        }
    }

    public class StudentConstructor
    {
        public StudentConstructor()
        { }

        public void Study(string name)
        {

            Console.WriteLine(name + "複習C#");
        }
    }


    // 泛型類約束,這裡T派生自泛型型別K,該型別稱為裸型別約束
    public class StudentT<T> where T : StudentK<StudentsInfo>
    {
        StudentK<StudentsInfo> skinfo = new StudentK<StudentsInfo>();
        public void Studys(string name)
        {
            string Name = "&TK泛型";
            skinfo.Study(Name);
        }
    }

    public class StudentK<K> where K : class
    {
        public void Study(string name)
        {
            Console.WriteLine(name + "會議進行中");
        }
    }
}