C # 1.x implements "unique arraylist of strongly typed elements"

Source: Internet
Author: User
Tags repetition

// For more information, see modify the marchlibrary of csdn ccat.
// C # 1.x implements "unique arraylist of strongly typed elements"
Using system;
Using system. collections;

/// The type of any element should be equal to the specified type or be a subclass of the specified type.
/// For the value type, it is best to pack it before use.
/// Used as a substitute before the C # 1.x language implements the generic function (runtime error ).
// Method 1: Implemented by inheriting from system. Collections. arraylist
Public class strongtypeuniquearraylist: arraylist
{
Private type _ type;
/// <Summary>
/// Disable Default Construction.
/// </Summary>
Private strongtypeuniquearraylist ()
{
}
/// <Summary>
/// Specify the element type during container Initialization
/// </Summary>
/// <Param name = "elementtype"> arraylist (element) type </param>
Public strongtypeuniquearraylist (system. Type elementtype)
{
_ Type = elementtype;
}
/// <Summary>
/// The internal element type cannot be changed.
/// </Summary>
Public 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 repetition", "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 repetition", "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 repetition", "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); // The element is repeated.
}
}

// Method 2: not implemented by inheriting from system. Collections. arraylist
//《Refactoring: improving the design of existing code
//3.21 refused bequest: replace inheritance with Delegation

Using system;
Using system. collections;
Public class class1
{
Private Static int I = 0;
Public String xxx = "test ";
Public class1 ()
{
System. Console. writeline (I ++ );
Xxx = I. tostring ();
}
Public override string tostring ()
{
Return xxx;
}
Static void main (string [] ARGs)
{
System. Console. writeline ("Hello World ");
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 (system. type. GetType ("class1 "));
System. Console. writeline (X. type );
X. Add (C1 );
X. Add (C2 );
X. Add (C3 );
X. insert (0, C4 );
System. Collections. ienumerator Ie = x. getenumerator ();
While (ie. movenext ())
System. Console. writeline (ie. Current. tostring ());
X [0] = C4;
While (ie. movenext ())
System. Console. writeline (ie. Current. tostring ());
X. Reset ();
Foreach (Object o in X)
System. Console. writeline (O. tostring () + "each ");
}
}

Public class strongtypeuniquearraylist: system. Collections. ienumerator //, system. Collections. ienumerable // supports foreach
{
Private arraylist al;
Private type _ type;
Private int _ Index =-1;

Public strongtypeuniquearraylist (system. Type elementtype)
{
Al = new arraylist ();
_ Type = elementtype;
}

Public System. Collections. ienumerator getenumerator () // supports iteration and foreach
{
Return this;
}

Public bool movenext () // supports foreach
{

Return ++ _ index <Al. count;
}
Public void reset () // supports foreach
{
_ Index =-1;
}
Public object current // supports foreach
{
Get
{
Return al [_ Index];
}
}

Public type
{
Get
{
Return _ type;
}
}
Private bool ismatchtype (type etype)
{
Return (etype ==_ type) | (etype. issubclassof (_ type ));
}
Public object this [int Index]
{
Set
{
If (ismatchtype (value. GetType ()))
{
If (Al. Contains (value ))
{
If (Al. indexof (value) = index)
{
Al [Index] = value;
}
Else
{
Throw new argumentexception (value. tostring () + "element repetition", "value ");
}
}
Else
{
Al [Index] = value;
}
}
Else
{
Throw new argumentexception (value. GetType (). fullname + "type error", "value ");
}
}
Get
{
Return al [Index];
}
}
Public int add (object value)
{
If (! Al. Contains (value) & ismatchtype (value. GetType ()))
{
Al. Add (value );
Return 1;
}
Else
{
Throw new argumentexception (value. GetType (). fullname + "type error or" + value. tostring () + "element repetition", "value ");
}
}
Public void insert (INT index, object value)
{
If (! Al. Contains (value) & ismatchtype (value. GetType ()))
{
Al. insert (index, value );
}
Else
{
Throw new argumentexception (value. GetType (). fullname + "type error or" + value. tostring () + "element repetition", "value ");
}
}
Public void insertrange (INT index, icollection C)
{
System. Collections. ienumerator Ie = C. getenumerator ();
While (ie. movenext ())
{
If (Al. Contains (ie. Current) |! Ismatchtype (ie. Current. GetType ()))
{
Throw new argumentexception (ie. Current. GetType (). fullname + "type error or" + IE. Current. tostring () + "element repetition", "C ");
}
}
Al. insertrange (index, C );
}
Public void setrange (INT index, icollection C)
{
System. Collections. ienumerator Ie = C. getenumerator ();
Int I = 0;
While (ie. movenext ())
{
If (ismatchtype (ie. Current. GetType ()))
{
If (Al. Contains (ie. Current) & Al. 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 ++;
}
Al. setrange (index, C );
}
Public void addrange (icollection C)
{
Foreach (Object o in Al)
{
If (Al. Contains (o) |! Ismatchtype (O. GetType ()))
{
Throw new argumentexception (O. GetType (). fullname + "type error or" + O. tostring () + "element repetition", "C ");
}
}
Al. addrange (C );
}
}

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.