当前位置:网站首页>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);
}
}边栏推荐
- 难道Redis真的变慢了吗?
- Example of video switching playback (video switching example) code
- tiup install
- Our company has used gateway services for 6 years, dynamic routing, authentication, current limiting, etc., a stable batch!
- 几种常见的存储器
- 面试何惧调优!腾讯技术官私藏的性能优化方案手册,原理实战齐全
- 本地事务与分布式事务
- 数据分析工具篇——HQL中DDL操作&DML操作
- Configuration - Notes
- [Cloud native] Alibaba Cloud ARMS business real-time monitoring
猜你喜欢

Our company has used gateway services for 6 years, dynamic routing, authentication, current limiting, etc., a stable batch!

Sentinel

Redis cache penetration, breakdown, avalanche and consistency issues

flask获取post请求参数

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

Flask入门学习教程

Mysql database query is very slow. Besides the index, what else can be caused?

华为「天才少年」计划招募的博士们,迎来首秀!

Flask之路由(app.route)详解

The Prospects of the Metaverse and the Four Tracks
随机推荐
Mysql数据库查询好慢,除了索引,还能因为什么?
一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?
SEATA分布式事务
华为云重磅发布开源软件治理服务——软件成分分析
tiup install
二、判断 & 循环
RISC-V调用惯例
tiup help
Delayed message queue
Packages - Notes
Flask之路由(app.route)详解
Introduction to TiUP
L2-007 家庭房产(vector、set、map的使用)
视频切换播放的例子(视频切换范例)代码
Flask入门学习教程
How to do a good job in technology selection
yarn的安装及使用教程
GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?
(Crypto必备干货)详细分析目前NFT的几大交易市场
[Cloud native] Grayscale release, blue-green release, rolling release, grayscale release explanation