A set provides a method to format and store any object. a set can be defined as one that implements one or more systems. collections. icollection, system. collections. idictionary and system. collections. the ilist object. The following are common collections of. Net:
- System. Collections. arraylist
- System. Collections. Stack
- System. Collections. Queue
- System. Collections. hashtable
Arraylist The arraylist Arraylist myal = new arraylist (); Myal. Add ("hello "); Myal. Add ("world "); Myal. Add ("! ");Console. writeline (myal [0] + myal [1] + myal [2] Stack Use the stack // Creates and initializes a new stack. Stack mystack = new stack (); Mystack. Push ("hello "); Mystack. Push ("world "); Mystack. Push ("! ");// Displays the properties and values of the stack. Console. writeline ("mystack "); Console. writeline ("\ tcount: {0}", mystack. Count ); Console. Write ("\ tvalues :"); System. Collections. ienumerator myenumerator = mystack. getenumerator (); While (myenumerator. movenext ()) Console. Write ("\ t {0}", myenumerator. Current ); Console. writeline (); Queue When you need a first-in-first-out set, Use Queue // Creates and initializes a new queue. Queue myq = new Queue (); Myq. enqueue ("hello "); Myq. enqueue ("world "); Myq. enqueue ("! ");// Displays the properties and values of the queue. Console. writeline ("myq "); Console. writeline ("\ tcount: {0}", myq. Count ); Console. Write ("\ tvalues :"); Hashtable If you need an array that can be queried by key value, use hashtable // Creates and initializes a new hashtable. Hashtable myht = new hashtable (); Myht. Add ("first", "hello "); Myht. Add ("second", "world "); Myht. Add ("third ","! ");// Displays the properties and values of the hashtable. Console. writeline ("myht "); Console. writeline ("\ tcount: {0}", myht. Count ); Console. writeline ("\ t values :");
Idictionaryenumerator myenumerator = myht. getenumerator (); Console. writeline ("\ t-key-\ t-value -"); While (myenumerator. movenext ()) Console. writeline ("\ t {0 }:\ t {1}", myenumerator. Key, myenumerator. value ); Console. writeline (); Of course, in addition to the. NET custom collections, you can also define your own collection classes by implementing ilist, icollection, and ienumerable. For example, datatable. Rows implements the icollection and ienumerable interfaces. However, this work is cumbersome and requires many implementation methods. Storage and indexing are also required. Therefore, the usual practice is to directly inherit from arraylist, stack, queue, and hashtable for further encapsulation, such: Public class users: arraylist { Public users () { }Public new user this [int Index] { Get { Return (User) (base [Index]); } Set { Base [Index] = value; } } Public int add (user value) { Return base. Add (value ); } } Public class user { Public user () { // // Todo: Add constructor logic here // } Private string m_strname; Private int m_intage; Private string m_stremail; Public string name { Get { Return this. m_strname; } Set { This. m_strname = value; } } Public int age { Get { Return this. m_intage; } Set { This. m_intage = value; } } Public String eamil { Get { Return this. m_stremail; } Set { This. m_stremail = value; } } } This increases readability, implementation, and convenience. You can directly use objusers [0]. Name to access |