范范(10)

来源:互联网 发布:联合石油数据库 在哪看 编辑:程序博客网 时间:2024/06/16 12:32

generic

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;namespace ConsoleApplication4{    struct Person_struct    {    }    class Person_class      {    }    class GenericList<T>  where T : struct      //只接受值类型的泛型参数    {       public void Add(T input)        {                 }    }        class GenericList_ref<T> where T : class     //只接受引用类型的泛型参数(class|interface|delegate|array|string)    {        public void Add(T input)        {        }    }       class Program    {        static void Main(string[] args)        {            List<String> list = new List<String>();    //List就是一个可以实现泛型的collection。                GenericList<int> intList = new GenericList<int>();  //int是值类型            GenericList<Person_struct> PersonList = new GenericList<Person_struct>();              GenericList_ref<String> StringList = new GenericList_ref<String>();   //string 是reference            GenericList_ref<Person_class> personlist2 = new GenericList_ref<Person_class>(); //class 是reference        }    }}
</pre><pre code_snippet_id="1652900" snippet_file_name="blog_20160419_3_8274490" name="code" class="csharp">
<pre name="code" class="java">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;namespace ConsoleApplication4{    public class Car    {        public virtual void drive(string str)        {        }    }    public class Toyota : Car    {        public override void drive(string str)        {            Console.WriteLine(str + " driving...");        }    }    public class Nissan : Car    {        public override void drive(string str)        {            Console.WriteLine(str + " driving...");        }    }    public class Driver<TCar> where TCar : Car, new()    {        public TCar mycar = new TCar();        public void start()        {            mycar.drive(mycar.GetType().ToString());        }    }        class Program        {            static void Main(string[] args)            {                Driver<Toyota> driver = new Driver<Toyota>();                driver.start();                Console.ReadLine();            }        }    }


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;namespace ConsoleApplication4{      class Person : IComparable<Person>    {        public string name;        public int CompareTo(Person other)        {            if (this.name.Equals(other.name))                return 0;            else                return 1;            throw new NotImplementedException();        }    }    class Program    {        public static void Test<T>(T s, T t) where T : IComparable<T>        {            System.Console.WriteLine(s.CompareTo(t));        }        static void Main(string[] args)        {            string s1 = "a";            string s2 = "a";            Test(s1, s2);            Person p1 = new Person();            Person p2 = new Person();            p1.name = "hah";            p2.name = "hah";            Test(p1, p2);            Console.ReadLine();        }    }}

0 0