C # 1.x Implement "Strong Type Element Unique ArrayList"

xiaoxiao2021-03-06  81

// This article refers to the 9CBS CCAT's Marchlibrary Modify // C # 1.x Implement "Strong Type Element Unique ArrayList" Using System; use system.collections; /// any element's TYPE should be equal to the specified type or for the specified type Subclass. /// For value type, it is best to pack it first. /// As a c # 1.x language to implement the use of generic function (runtime error). // Method 1: Public Class StrongTypeUniqueRaylist: arraylist {private typearraylist: arraylist {private typearraylist: arraylist {private typearraylist: arraylist {private typeraylist: arraylist {private type_tylist: array; /// private strongTypeUniqueRayList () {} ///

/// Specify its element type /// public strongtypeUniqueRayList (System.Type ElementType) {_type = elementtype;} /// /// cannot change its internal element type. /// public type type {get {return_type;}} private bool ismatchtype (type type) {return (eType ==_Type) | (ETYPE.issubclassof (_type));}

Public override object this [int index] {set {ix (ismatchtype (Value.gettype ()) {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 address {if (! base.contains (value) && ismatchType (Value.GetType ())) {base.add (value); returnif 1;} else {throw new argumentException (value.gettype ) .Fullname "type error or" value.toTString () "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 ) "Elements 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 Repeat", "C");}} .InsertRange (INDEX, C); PUBLIC OVERRIDE VOID SetRream (int index, icollection c) {system.collections.ienumerator ie = c.GeteNumerator (); int i = 0; while (IE.MOVENEXT ()) {IF ISMATCHTYPE (IE.current.gettype ())) {if (ie.current) && base.indexof (ie.current)! = I) {throw new argumentException (IE.current.tostring () element Repeat "," 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 () "Elements Repeat", "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 (); // strongtypeuniqueRayList 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}} // Method 2: Do not inherit in System.Collections.Arraylist // "refactoring: improving the design of evigning 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 // support 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.}

Public Bool MoveNext () // Support for Foreach {Return _Dex

public Type 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 Repeat", "Value");}} else {al [index] = value;}} else {throw new argumentexception (value.gettype (). Fullname "Type Error", "Value");} } Get {return al [index];}} public int address {if (! Al.contains (value) && ismatchtype (Value.GetType ())) {al.add (value); returnif 1;} Else {throw new argumentException (value.gettype (). fullname "type error or" value.toTString () "Element Repeat", "Value");}} public void insert (int index, object value) {ix (! Al.Contains (Value) && ismatchType (Value.getType ())) {al.insert (index, value);} else {throw new a Rgumentexception (Value.gettype (). FullName "Type Error or" Value.Tostring () "Elements Repeat", "Value");}} PUBLIC VOID INSERTRANGE (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.toTString () " Element Repeat "," C ");}} Al.IsertRange (INDEX, C);} Public Void SetRange (int index, iCollection c) {system.collections.ienumerator ie =

转载请注明原文地址:https://www.9cbs.com/read-94531.html

New Post(0)