范范(10)
2016-04-14 16:29
369 查看
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(); } } }
相关文章推荐
- 二叉排序树(BST)创建,删除,查找操作
- 详解JSP中的语句对象Statement操作MySQL的使用实例
- mysql登录报错提示:ERROR 1045 (28000)的解决方法
- 通过Android源代码分析startActivity()过程(下)
- 使用virtualbox设置双网卡,桥接+内部网络
- C++中map、set、hash_map、hash_set、unordered_map、unordered_set通俗辨析
- 利用反射取得类中属性(引用及非引用数据类型)及设置该类实例化对象的属性值
- Ajax例子,views返回,html接收数据
- HTML5 五子棋 - JS/Canvas 游戏
- 树结点的简单练习
- 二叉排序树(BST)创建,删除,查找操作
- URL链接中文参数乱码的若干处理方法
- tesseract的编译安装
- 静态变量static
- 如何在spring中获取request对象
- 关于怎么打开/data目录查看SharedPreferences数据文件
- 11的阶乘
- json样式转化
- android 数据库 sqlite数据类型(时间 日期 double等)
- strstr()函数自实现