泛型(泛型类,泛型方法)及字典

本文通过实例展示了C#中的泛型方法、泛型类及泛型集合的使用方式,包括List、Dictionary、Queue和Stack等常用集合类的操作,并提供了一个简单的泛型交换方法的实现。

protected void Page_Load(object sender, EventArgs e)
{
#region 泛型方法
////int a = 10;
////int b = 20;
////this.Swap(ref a, ref b);
//////Swap(ref a,ref b);
////Response.Write(string.Format("a:{0};b:{1}", a, b));
#endregion

#region 泛型类
////Point<int> p = new Point<int>(10, 20);
////Response.Write(p.ToString());
////Point<float> pf = new Point<float>(12.2f, 23.2f);
////Response.Write(pf.ToString());
#endregion

#region List<T>
//List<Person> list = new List<Person>();
//list.Add(new Person("张", "三"));
//list.Add(new Person("李","四"));
//list.Add(new Person("王", "五"));

//Response.Write(list[0].FirstName + list[0].LastName);
//Response.Write("<br/>-------------<br/>");
//for (int i = 0; i < list.Count; i++)
//{
// Response.Write(list[i].FirstName + list[i].LastName + "<br/>");
//}
#endregion

#region dictionary<T>
//Dictionary<string, Person> dic = new Dictionary<string, Person>();
//dic.Add("zhang", new Person("张", "三"));
//dic.Add("li", new Person("李", "四"));
//dic.Add("wang", new Person("王", "五"));
//Response.Write(dic["wang"].FirstName + dic["wang"].LastName+"<br/>");
//foreach (string str in dic.Keys)
//{
// Response.Write(str + "<br/>");
//}
//foreach (Person p in dic.Values)
//{
// Response.Write(p.FirstName + p.LastName + "<br/>");
//}
#endregion
Queue<int> qu = new Queue<int>();
for (int i = 0; i < 10; i++){
qu.Enqueue(i);
}
for (int i = 0; i < 10; i++){
Response.Write(qu.Dequeue().ToString());
}
Response.Write("<br/>==================<br/>");
Stack<int> st = new Stack<int>();
for (int i = 0; i < 10; i++){
st.Push(i);
}
for (int i = 0; i < 10; i++){
Response.Write(st.Pop().ToString());
}
}
//public T Add<T>(T a, T b)
//{
// return a + b;
//}
public void Swap<T>(ref T a, ref T b)
{
T temp;
temp = a;
a = b;
b = temp;
}
//public void Swap(ref int a, ref int b)
//{
// int temp;
// temp = a;
// a = b;
// b = temp;
//}
//public void Swap(ref float a, ref float b)
//{
// float temp;
// temp = a;
// a = b;
// b = temp;
//}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///Person 的摘要说明
/// </summary>
public class Person:IComparable
{
private string firstName;
private string lastName;
public string LastName{
get { return lastName; }
set { lastName = value; }
}
public string FirstName{
get { return this.firstName; }
set { this.firstName = value; }
}
public Person(string fname, string lname){
this.firstName = fname;
this.lastName = lname;
}
//当需要进行等值性比较时,需要重写类的Equals方法
public override bool Equals(object obj)
{
Person p = obj as Person;
if (p == null)
throw new ArgumentException("obj is not a person");
if (p.firstName == this.firstName &&
p.lastName == this.lastName)
return true;
else
return false;
}

public Person()
{
//
//TODO: 在此处添加构造函数逻辑
//
}

#region IComparable 成员
public int CompareTo(object obj)
{
Person p = obj as Person;
if (p == null)
throw new ArgumentException("obj is not a person");
int first = this.firstName.CompareTo(p.firstName);
if (first == 0)
{
first = this.lastName.CompareTo(p.lastName);
return first;
}
else
return first;
}
#endregion
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值