国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C# 1.x 實現 強類型元素唯一的 ArrayList

2024-07-21 02:18:38
字體:
來源:轉載
供稿:網友
菜鳥學堂:
//本文參閱 csdn ccat 的 marchlibrary 修改
// c# 1.x 實現 "強類型元素唯一的 arraylist"
using system;
using system.collections;

/// 任何元素的 type 都應當等于指定的類型或為指定類型的子類。
/// 對于值類型,最好先行裝箱再使用。
/// 作為 c# 1.x 語言實現泛型功能之前的代用品(運行時錯誤)。

public class strongtypeuniquearraylist : arraylist
{
private type _type;
/// <summary>
/// 禁止默認構造。
/// </summary>
private strongtypeuniquearraylist()
{
}
/// <summary>
/// 在容器初始化時指定其元素類型
/// </summary>
/// <param name="elementtype">arraylist (元素)類型</param>
public strongtypeuniquearraylist(system.type elementtype)
{
_type = elementtype;
}
/// <summary>
/// 不能再更改其內部元素類型。
/// </summary>
public type type
{
get
{
return _type;
}
}
private bool ismatchtype(type etype)
{
return (etype == _type)|(etype.issubclassof(_type));
}


public override object this[int index]
{
set
{
if (ismatchtype(value.gettype()))
{
if (base.contains(value))
{
if (base.indexof(value) == index)
{
base[index] = value;
}
else
{
throw new argumentexception(value.tostring() + " 元素重復","value");
}
}
else
{
base[index] = value;
}
}
else
{
throw new argumentexception(value.gettype().fullname + " 類型錯誤", "value");
}
}
}

public override int add(object value)
{
if (!base.contains(value) && ismatchtype(value.gettype()))
{
base.add(value);
return 1;
}
else
{
throw new argumentexception(value.gettype().fullname + " 類型錯誤 或 " + value.tostring() + " 元素重復", "value");
//return -1;
}
}
public override void insert(int index, object value)
{
if (!base.contains(value) && ismatchtype(value.gettype()))
{
base.insert(index,value);
}
else
{
throw new argumentexception(value.gettype().fullname + " 類型錯誤 或 " + value.tostring() + " 元素重復", "value");
}
}

public override void insertrange(int index, icollection c)
{
system.collections.ienumerator ie = c.getenumerator();
while (ie.movenext())
{
if (base.contains(ie.current) || !ismatchtype(ie.current.gettype()))
{
throw new argumentexception(ie.current.gettype().fullname + " 類型錯誤 或 " + ie.current.tostring() + " 元素重復", "c");
}
}
base.insertrange(index,c);
}
public override void setrange(int index, icollection c)
{
system.collections.ienumerator ie = c.getenumerator();
int i = 0;
while (ie.movenext())
{
if (ismatchtype(ie.current.gettype()))
{
if (base.contains(ie.current) && base.indexof(ie.current) != i)
{
throw new argumentexception(ie.current.tostring() + " 元素重復","c");
}
}
else
{
throw new argumentexception(ie.current.gettype().fullname + " 類型錯誤", "c");
}
i++;
}
base.setrange(index,c);
}
public override void addrange(icollection c)
{
system.collections.ienumerator ie = c.getenumerator();
while (ie.movenext())
{
if (base.contains(ie.current) || !ismatchtype(ie.current.gettype()))
{
throw new argumentexception(ie.current.gettype().fullname + " 類型錯誤 或 " + ie.current.tostring() + " 元素重復", "c");
}
}
base.addrange(c);
}
}
//test:
public class class1
{
private static int i = 0;
public string xxx = "test";
public class1()
{
system.console.writeline(i++);
}
static void main(string[] args)
{
system.console.writeline("hello world");
class1 c1 = new class1();
class1 c2 = new class1();
class1 c3 = new class1();
class1 c4 = new class1();
//strongtypeuniquearraylist x = new strongtypeuniquearraylist(typeof(class1));
strongtypeuniquearraylist x = new strongtypeuniquearraylist(system.type.gettype("class1"));
system.console.writeline(x.type);
x.add(c1);
x.add(c2);
x.add(c3);
x.insert(0,c4);
//x.add(new class1());
//x.add(c1);
// x[2]= new object();
//x.insert(2,new object()); //類型錯誤
//x.insert(2,c2);
// x.add(c2); //元素重復
}
}


上一篇:c#的開發環境

下一篇:c# encrypt

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 淮南市| 麻栗坡县| 清新县| 甘孜县| 城步| 锦屏县| 福泉市| 高淳县| 历史| 台州市| 郸城县| 凌云县| 遂溪县| 桂东县| 临泉县| 北安市| 康保县| 米泉市| 阿拉尔市| 贡觉县| 宿州市| 黄陵县| 马公市| 福安市| 德钦县| 合山市| 贵南县| 木里| 容城县| 板桥市| 德安县| 资兴市| 枝江市| 深圳市| 哈尔滨市| 阿瓦提县| 胶南市| 洛宁县| 红河县| 靖远县| 若尔盖县|