当前位置:网站首页>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);
}
}边栏推荐
- Flask之路由(app.route)详解
- 华为云重磅发布开源软件治理服务——软件成分分析
- 4 senior experts share the insider architecture design and implementation principles of Flink technology with years of experience in large factories
- 视频切换播放的例子(视频切换范例)代码
- 编译、链接 - 笔记 - 3
- TiUP FAQ
- Classes and Objects (Part 2)
- Distributed pre-course: MySQL implements distributed locks
- yarn的安装及使用教程
- RISC-V调用惯例
猜你喜欢

Sentinel

Mysql数据库查询好慢,除了索引,还能因为什么?

How is the B+ tree index page size determined?

Classes and Objects (Part 2)

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

Installing and Uninstalling MySQL on Mac

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

Placement Rules 使用文档
![[Cloud native] Grayscale release, blue-green release, rolling release, grayscale release explanation](/img/90/f7d85ac655d9206fefbd28e0fc81ba.png)
[Cloud native] Grayscale release, blue-green release, rolling release, grayscale release explanation

70行代码撸一个桌面自动翻译神器
随机推荐
Debug - Notes
4 senior experts share the insider architecture design and implementation principles of Flink technology with years of experience in large factories
Introduction to golang image processing library image
The Prospects of the Metaverse and the Four Tracks
About the data synchronization delay of MySQL master-slave replication
vite 多页面应用刷新页面时,不会在当前路由中,会返回到根路由
数据库日期类型字段设计,应该如何选择?
JVM performance tuning
Local Transactions vs Distributed Transactions
QIIME2得到PICRUSt2结果后如何分析
Excel uses Visual Basic Editor to modify macros
GeoServer
2022最新 | 室外单目深度估计研究综述
TensorFlow自定义训练函数
GeoServer + openlayers
【为宏正名】99%的人从第一天学习C语言就自废的武功
Changing SELECT...FROM to FROM...SELECT doesn't 'fix' SQL
华为「天才少年」计划招募的博士们,迎来首秀!
100w的数据表比1000w的数据表查询更快吗?
Distributed pre-course: MySQL implements distributed locks