当前位置:网站首页>C# List<T> 模板的案例
C# List<T> 模板的案例
2022-07-30 15:08:00 【laocooon】
public class List<T>
{
const int defaultCapacity = 4; //常量
T[] items;//字段
int count; //字段
public List(int capacity = defaultCapacity)//构造函数
{
items = new T[capacity];
}
public int Count//属性
{
get { return count; }
}
public int Capacity//属性
{
get
{
return items.Length;
}
set
{
if (value < count) value = count;
if (value != items.Length)
{
T[] newItems = new T[value];
Array.Copy(items, 0, newItems, 0, count);
items = newItems;
}
}
}
public T this[int index]//索引器
{
get
{
return items[index];
}
set
{
items[index] = value;
OnChanged();
}
}
public void Add(T item)//方法
{
if (count == Capacity) Capacity = count * 2;
items[count] = item;
count++;
OnChanged();
}
protected virtual void OnChanged()//方法
{
if (Changed != null) Changed(this, EventArgs.Empty);
}
public override bool Equals(object other)//方法
{
return Equals(this, other as List<T>);
}
static bool Equals(List<T> a, List<T> b)//方法
{
if (a == null) return b == null;
if (b == null || a.count != b.count) return false;
for (int i = 0; i < a.count; i++)
{
if (!object.Equals(a.items[i], b.items[i]))
{
return false;
}
}
return true;
}
public event EventHandler Changed; //事件
public static bool operator ==(List<T> a, List<T> b)//运算符
{
return Equals(a, b);
}
public static bool operator !=(List<T> a, List<T> b)
{
return !Equals(a, b);
}
}边栏推荐
猜你喜欢

GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?

嵌入式开发:嵌入式基础知识——正确启动固件项目的 10 条建议

952. 按公因数计算最大组件大小 : 枚举质因数 + 并查集运用题

【云原生 • DevOps】influxDB、cAdvisor、Grafana 工具使用详解

Sparse-PointNet: See Further in Autonomous Vehicles 论文笔记

关于mariadb/mysql的user表:密码正确但登录失败,可能与mysql的空用户有关

GeoServer

nodejs环境变量设置

Lock wait timeout exceeded solution

70行代码撸一个桌面自动翻译神器
随机推荐
(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
90后人大硕士为学医竟重新高考,成功被首医大录取
代码随想录笔记_哈希_1l两数之和
Introduction to TiUP
CS内网横向移动 模拟渗透实操 超详细
tiup completion
如何做好技术选型
Load Base Split 使用文档
tiup list
100w的数据表比1000w的数据表查询更快吗?
GeoServer + openlayers
TiUP FAQ
TiUP 故障排查
Flask入门学习教程
科研中一些常用软件清单
B+树索引页大小是如何确定的?
Mysql database query is very slow. Besides the index, what else can be caused?
华为「天才少年」计划招募的博士们,迎来首秀!
Alluxio为Presto赋能跨云的自助服务能力
让人上瘾的新一代开发神器,彻底告别Controller、Service、Dao等方法