C # 1.x implementation of strongly-typed elements unique ArrayList

Source: Internet
Author: User
Tags contains insert repetition tostring
This article see Marchlibrary modification of CSDN Ccat
C # 1.x implements the ArrayList of strongly typed elements
Using System;
Using System.Collections;

The type of any element should be equal to the specified type or to a subclass of the specified type.
For value types, it is best to use boxing in advance.
A substitute (Run-time error) for implementing generic functionality as a C # 1.x language.

public class Strongtypeuniquearraylist:arraylist
{
Private Type _type;
<summary>
Prevents default constructs.
</summary>
Private Strongtypeuniquearraylist ()
{
}
<summary>
Specifying its element type when the container is initialized
</summary>
<param name= "ElementType" >arraylist (Element) type </param>
Public strongtypeuniquearraylist (System.Type ElementType)
{
_type = ElementType;
}
<summary>
The intrinsic element type cannot be changed again.
</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 () + "element Repeat", "value");
}
}
Else
{
Base[index] = value;
}
}
Else
{
throw new ArgumentException (value. GetType (). FullName + "type error", "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 + "type error or" + value. ToString () + "element Repeat", "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 + "type error or" + value. ToString () + "element Repeat", "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 + "type error or" + ie. Current.tostring () + "element Repetition", "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 () + "element Repetition", "C");
}
}
Else
{
throw new ArgumentException (ie. Current.gettype (). FullName + "type error", "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 + "type error or" + ie. Current.tostring () + "element Repetition", "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 ()); Type error
X.insert (2,C2);
X.add (C2); Element repeat
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.