范范(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
- 范范(10)
- 范范(10)
- 范范(5)
- 范范(6)+ 作业
- 范范(7)
- 范范(8)
- 读书笔记 (2005.10.10)
- (谭版)10 指针
- J2EE Architecture(10)
- 谋划(10)
- 谋划(10)
- 算法、排序(10)
- API 测试(10)
- Python笔记(10)
- 一些技巧(10)
- java(10)
- 自定义标签(10)
- (10)泛型
- 创建Chrome浏览器插件
- 静态变量static
- 10个随机数相加
- 使用fuel6.0自动安装openstack-juno版本
- 利用反射取得类中属性(引用及非引用数据类型)及设置该类实例化对象的属性值
- 范范(10)
- 练手
- vb14
- Mybatis中oracle、mysql、db2、sql server的like模糊查询
- 剑指offer-面试题44:扑克牌的顺子
- 【目标检测】Faster RCNN算法详解
- 8种交换
- vb
- Angluar中Controller之间通信方法