范范(10)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值