惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
T
Threat Research - Cisco Blogs
V
Vulnerabilities – Threatpost
T
Tor Project blog
T
Troy Hunt's Blog
C
CERT Recently Published Vulnerability Notes
C
Cisco Blogs
W
WeLiveSecurity
Cloudbric
Cloudbric
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
爱范儿
爱范儿
Google Online Security Blog
Google Online Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Martin Fowler
Martin Fowler
Cisco Talos Blog
Cisco Talos Blog
F
Full Disclosure
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
I
Intezer
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
B
Blog RSS Feed
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
腾讯CDC
L
LangChain Blog
L
LINUX DO - 热门话题
H
Help Net Security
S
Schneier on Security
N
Netflix TechBlog - Medium
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
Spread Privacy
Spread Privacy
S
Secure Thoughts
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
P
Privacy & Cybersecurity Law Blog
Cyberwarzone
Cyberwarzone
A
About on SuperTechFans
NISL@THU
NISL@THU
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
Recorded Future
Recorded Future
雷峰网
雷峰网
AWS News Blog
AWS News Blog
V2EX - 技术
V2EX - 技术

博客园 - hulu

从一个int值显示相应枚举类型的名称或者描述 第8章 商品目录管理 第4章 创建商品目录 第2,第三章 概要 Scott Mitchell的ASP.NET2.0数据指南中文版索引 如何自定义gridview的表格显示? 7/14 原型编码阶段里程碑完成 asp.net2.0 的 gridview 7/9 原型编码阶段: (6) 使用DetailsView和FormView(7-5 11:54) - hulu 7/5 原型编码阶段: (5) 使用ADO.NET 7/3 原型编码阶段: (4) GridView的编辑更新 成功软件开发者的9种编程习惯(一) 6/29 原型编码阶段:(3) CommandField,TemplateField 6/29 原型编码阶段:(2) GridView的数据库操作 6/29 原型编码阶段:(1)连接并访问数据库 6/29 项目目录结构有所调整 6/28 项目原型前期实现任务确定 C# 2.0 除了泛型
C# 2.0 泛型
hulu · 2007-06-27 · via 博客园 - hulu

作者:webabcd

介绍
泛型是 C# 2.0 的最强大的功能。通过泛型可以定义类型安全的数据结构,而无须使用实际的数据类型。这能够显著提高性能并得到更高质量的代码,因为您可以重用数据处理算法。

泛型类

/*---
 * 如何 使用 和 继承 泛型类
---
*/

using System;
using System.Collections.Generic;

// 泛型类
public class GenericClass<T>
{
    
// 返回泛型的具体类型和ToString()后的值
    public virtual string Output(T t)
    
{
        
return "类型:" + t.GetType().ToString() + ";值:" + t.ToString();
    }

}


// 继承自一个泛型类,指定基类的泛型的具体类型
public class InheritClass1 : GenericClass<string>
{

}


// 继承自一个泛型类,基类和子类都是同一泛型
public class InheritClass2<T> : GenericClass<T>
{
    
public override string Output(T t)
    
{
        
return "子类实现-类型:" + t.GetType().ToString() + ";值:" + t.ToString();
    }

}


// 继承自一个泛型类,指定基类的泛型的具体类型,子类仍然可以是泛型
public class InheritClass3<Z> : GenericClass<double>
{
    
public string ChildOutput(Z z)
    
{
        
return "子类实现-类型:" + z.GetType().ToString() + ";值:" + z.ToString();
    }

}



public partial class Generic_Class : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        GenericClass
<DateTime> c = new GenericClass<DateTime>();
        Response.Write(c.Output(DateTime.Now) 
+ "<br />");

        InheritClass1 c1 
= new InheritClass1();
        Response.Write(c1.Output(
"abc"+ "<br />");

        GenericClass
<Guid> c2 = new InheritClass2<Guid>();
        Response.Write(c2.Output(
new Guid()) + "<br />");

        InheritClass3
<int> c3 = new InheritClass3<int>();
        Response.Write(c3.Output(Math.PI) 
+ " ||| " + c3.ChildOutput(123+ "<br />");
    }

}

运行结果
类型:System.DateTime;值:2007-2-10 22:56:09
类型:System.String;值:abc
子类实现-类型:System.Guid;值:00000000-0000-0000-0000-000000000000
类型:System.Double;值:3.14159265358979 ||| 子类实现-类型:System.Int32;值:123

泛型方法

/*---
 * 如何 使用 泛型方法 
---
*/

using System;
using System.Collections.Generic;

public class GenericMethod
{
    
// 静态 泛型方法
    public static string Output<T>(T t)
    
{
        
return "类型:" + t.GetType().ToString() + ";值:" + t.ToString();
    }

}



public partial class Generic_Method : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        Response.Write(GenericMethod.Output
<int>(23+ "<br />");
        Response.Write(GenericMethod.Output
<DateTime>(DateTime.Now) + "<br />");
    }

}

运行结果
类型:System.Int32;值:23
类型:System.DateTime;值:2007-2-10 22:57:29

泛型委托

/*---
 * 如何 使用 泛型委托
---
*/

using System;
using System.Collections.Generic;

public class GenericDelegate
{
    
// 声明一个泛型委托
    public delegate string OutputDelegate<T>(T t);

    
// 定义一个静态方法
    public static string DelegateFun(string s)
    
{
        
return String.Format("Hello, {0}", s);
    }


    
// 定义一个静态方法
    public static string DelegateFun(DateTime dt)
    
{
        
return String.Format("Time, {0}", dt.ToString());
    }

}



public partial class Generic_Delegate : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
// 使用泛型委托
        GenericDelegate.OutputDelegate<string> delegate1
            
= new GenericDelegate.OutputDelegate<string>(GenericDelegate.DelegateFun);

        Response.Write(delegate1(
"aabbcc"));
        Response.Write(
"<br />");

        
// 使用泛型委托(匿名方法)
        GenericDelegate.OutputDelegate<DateTime> delegate2 = GenericDelegate.DelegateFun;
        Response.Write(delegate2(DateTime.Now));
    }

}

运行结果
Hello, aabbcc
Time, 2007-2-10 22:59:26

抽象泛型类,派生约束

/*---
 * 如何 使用 和 继承 抽象泛型类 
 * 派生约束
---
*/

using System;
using System.Collections.Generic;

// 泛型抽象类
public abstract class GenericParent
{
    
// 泛型抽象方法,返回值为一个泛型,加一个约束使泛型X要继承自泛型Y
    public abstract X Output<X, Y>(X x, Y y) where X : Y;

    
// 泛型抽象方法,返回值为一个string类型,加一个约束使泛型X要继承自泛型Y
    public abstract string Output2<X>(X x) where X : System.ComponentModel.IListSource;
}


public class GenericChild : GenericParent
{
    
// 重写抽象类的泛型方法
    public override T Output<T, Z>(T t, Z z)
    
{
        
return t;
    }


    
// 重写抽象类的泛型方法
    public override string Output2<T>(T t)
    
{
        
return t.GetType().ToString();
    }

}



public partial class Generic_Abstract : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        GenericChild gc 
= new GenericChild();
        Response.Write(gc.Output
<string, IComparable>("aaa""xxx"));
        Response.Write(
"<br />");

        Response.Write(gc.Output2
<System.Data.DataTable>(new System.Data.DataTable()));
        Response.Write(
"<br />");
    }

}

运行结果
aaa
System.Data.DataTable

泛型接口,派生约束,构造函数约束

/*---
 * 如何 使用 泛型接口
 * 派生约束
 * 构造函数约束(如果实例化的话)
---
*/

using System;
using System.Collections.Generic;

// 泛型接口
public interface IGenericInterface<T>
{
    T CreateInstance();
}


// 实现上面泛型接口的泛型类
// 派生约束where T : TI(T要继承自TI)
// 构造函数约束where T : new()(T可以实例化)
public class Factory<T, TI> : IGenericInterface<TI>
    where T : TI, 
new()
{
    
public TI CreateInstance()
    
{
        
return new T();
    }

}



public partial class Generic_Interface : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        IGenericInterface
<System.ComponentModel.IListSource> factory =
            
new Factory<System.Data.DataTable, System.ComponentModel.IListSource>();

        Response.Write(factory.CreateInstance().GetType().ToString());
        Response.Write(
"<br />");
    }

}

运行结果
System.Data.DataTable

其它

/*---
 * 泛型 其它
---
*/

using System;
using System.Collections.Generic;

// 泛型也可以使用别名
using MyList = System.Collections.Generic.List<string>;

public partial class Generic_Other : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        MyList ml 
= new MyList();
        ml.Add(
"aaa");
        ml.Add(
"bbb");

        Response.Write(ml[
0]);
        Response.Write(
"<br />");
        Response.Write(ml[
1]);

        
// 其它说明
        
// 值类型约束 public class MyClass<T> where T : struct { }
        
// 引用类型约束 public class MyClass<T> where T : class { }
        
// 没有泛型属性
    }

}

运行结果
aaa
bbb

OK

[源码下载]