C#泛型约束与调用
来源:互联网 发布:中信国安历史交易数据 编辑:程序博客网 时间:2024/06/08 06:01
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 + "会议进行中"); } }}
阅读全文
0 0
- C#泛型约束与调用
- c#泛型约束
- C#泛型约束
- C#泛型约束
- c#泛型约束
- C# 泛型约束
- c#泛型约束
- C# 泛型约束
- C#泛型约束
- C#泛型约束
- C# where泛型约束
- C#的泛型约束-
- C#中的泛型约束
- 浅析C# 泛型约束中的一般约束
- 3.5(接口,结构,泛型委托和泛型约束,多线程的调用与挂起)
- C# 泛型约束,泛型函数
- C#泛型学习笔记之一(约束)
- C#中的where泛型约束
- 文章标题
- 欢迎使用CSDN-markdown编辑器
- php和JavaScript实现八大排序算法
- PAT --- 1010. 一元多项式求导 (25)
- Java测试框架Mockito源码分析
- C#泛型约束与调用
- leetcode之二叉树类之二叉树中序遍历运用-----OJ173/230/98/99/285 二叉树迭代器/BST第K小元素/判断BST是否合法/恢复BST/二叉树下个节点
- Failed to decode downloaded font
- 跳跃游戏(一,二)
- 用empty()来代替检查size()是否为空
- SSH框架中文乱码终极解决方案
- Java后端易忽略的问题
- [笔记]ndarray切片(python)
- 常见的两种python编译器的安装