C # integrated use -- ArrayList,

Source: Internet
Author: User

C # integrated use -- ArrayList,

Set: You can use a set to maintain an object group.

Arrays in C # are implemented as instances of the System. Array class. They are only one type in the Collection class. A collection class is generally used to process object lists. It has more functions than a simple array and is mostly obtained by implementing interfaces in the System. Collections namespace,

Therefore, the syntax of the set has been standardized. This namespace also contains other interesting things, such as classes that implement these interfaces in different ways than System. Array. Functions of a set (including basic functions, such as accessing items in a set using the [index] syntax) can be implemented through interfaces,

This interface does not limit the use of basic collection classes, such as System. Array. On the contrary, we can also create our own custom collection classes. These sets can be used for objects to be enumerated (that is, the objects from which the set is to be created ). One advantage of this is the custom set.

The combination can be strongly typed. That is to say, when extracting items from a set, you do not need to convert them to the correct type.

 

Several Interfaces in the System. Collections namespace provide the basic combination function:

IEnumerable can iterate items in the set.
ICollection (inherited from IEnumerable) can obtain the number of items in the set and copy the items to a simple array type.
IList (inherited from IEnumerable and ICollection) provides a list of items in the Set, allows access to these items, and provides other basic functions related to the item list.
IDictionary (inherited from IEnumerable and ICollection) is similar to IList, but provides a list of items that can be accessed through key values (rather than indexes.

 

The System. Array class implements IList, ICollection, and IEnumerable, but does not support more advanced features of IList. It indicates a list of items with fixed sizes.

The basic usage of ArrayList is as follows:

1 static void Main (string [] args) 2 {3 // arrayList capacity: 10 4 ArrayList arrayList1 = new ArrayList (10); 5 6 // arrayList1 [0] = 1; // you cannot access ArrayList through indexes at this time. Otherwise, System is thrown. argumentOutOfRangeException 7 // Add Element 8 arrayList1.Add (1); 9 arrayList1.Add (2); 10 arrayList1.Add (3); 11 arrayList1.Add (4); 12 arrayList1.Add ("hello "); 13 arrayList1.Add ("ArrayList"); 14 15 arrayList1 [2] = 4; // you can access ArrayList16 17 by indexing. Console. writeLine ("arrayList1 count: {0}", arrayList1.Count); 18 19 foreach (object I in arrayList1) 20 {21 Console. writeLine ("arrayList1 item {0} type: {1}", I, I. getType (); 22} 23 24 Console. writeLine ("remove item at index 4"); 25 arrayList1.RemoveAt (4); 26 27 foreach (object I in arrayList1) 28 {29 Console. writeLine ("arrayList1 item {0} type: {1}", I, I. getType (); 30} 31 32 Console. writeLine ("remove Item 2 "); 33 arrayList1.Remove (2); 34 35 foreach (object I in arrayList1) 36 {37 Console. writeLine ("arrayList1 item {0} type: {1}", I, I. getType (); 38} 39 40 Console. writeLine ("\ n"); 41 // create ArrayList242 ArrayList arrayList2 = new ArrayList (ArrayList1) through arrayList1; 43 Console. writeLine ("arrayList2 count: {0}", arrayList1.Count); 44 foreach (object I in arrayList1) 45 {46 Console. writeLine ("arrayList2 ite M {0} type: {1} ", I, I. GetType (); 47} 48 49 Console. WriteLine (" \ n \ nPress any key to exit! "); 50 Console. ReadKey (); 51}

Running result:

arrayList1 count: 6arrayList1 item 1 type:System.Int32arrayList1 item 2 type:System.Int32arrayList1 item 4 type:System.Int32arrayList1 item 4 type:System.Int32arrayList1 item hello type:System.StringarrayList1 item ArrayList type:System.Stringremove item at index 4arrayList1 item 1 type:System.Int32arrayList1 item 2 type:System.Int32arrayList1 item 4 type:System.Int32arrayList1 item 4 type:System.Int32arrayList1 item ArrayList type:System.Stringremove item 2arrayList1 item 1 type:System.Int32arrayList1 item 4 type:System.Int32arrayList1 item 4 type:System.Int32arrayList1 item ArrayList type:System.StringarrayList2 count: 4arrayList2 item 1 type:System.Int32arrayList2 item 4 type:System.Int32arrayList2 item 4 type:System.Int32arrayList2 item ArrayList type:System.StringPress any key to exit!

  

Custom set

You can inherit System. collections. collectionBase class: Custom set. The CollectionBase class has the IEnumerable, ICollection, and IList interfaces, but only provides some brief implementation code, especially the Clear () and RemoveAt () Methods of IList,

  And the Count attribute of ICollection. If you want to use the provided functions, You need to execute other code by yourself.

To facilitate task execution, CollectionBase provides two protected attributes that can access the stored object itself. We can use List and InnerList. List can be accessed through the IList interface. InnerList is the ArrayList object used to store items.

1 public abstract class Animal 2 {3 private string name; 4 public string Name 5 {6 get {return name;} 7 set {name = value ;} 8} 9 10 public Animal () 11 {12 name = "NULL"; 13} 14 15 public Animal (string newName) 16 {17 name = newName; 18} 19 20 public void Feed () 21 {22 Console. writeLine ("{0} has been fed. ", name); 23} 24} 25 26 public class Dog: Animal27 {28 public void Run () 29 {30 Console. writeLine ("Dog run .... "); 31} 32 33 public Dog (string newName) 34: base (newName) 35 {36 37} 38} 39 40 public class Bird: animal41 {42 public Bird (string newName) 43: base (newName) 44 {45 46} 47 48 public void Fly () 49 {50 Console. writeLine ("Bird fly .... "); 51} 52} 53 54 class Animals: CollectionBase55 {56 public void Add (Animal animal) 57 {58 List. add (animal); 59} 60 61 public void Remove (Animal animal) 62 {63 Lis T. remove (animal); 64} 65 66 public Animals () 67 {68 69} 70 71 // use the following code to enable Animals to access 72 public Animal through indexes this [int animalIndex] 73 {74 get75 {76 return (Animal) list [animalIndex]; 77} 78 set79 {80 List [animalIndex] = value; 81} 82} 83} 84 85 static void Main (string [] args) 86 {87 Animals animals = new Animals (); 88 animals. add (new Dog ("Jack"); 89 animals. add (new Bird ("Jason"); 90 91 foreach (Animal animal I N animals) 92 {93 animal. Feed (); 94} 95 96 Console. WriteLine ("\ n \ nPress any key to exit! "); 97 Console. ReadKey (); 98}

Running result:

Jack has been fed.Jason has been fed.Press any key to exot!

The Add () and Remove () methods are implemented as strongly typed methods, and the standard Add () method used for access items in the IList interface is used. This method is only used to process Animal classes or classes derived from Animal. The ArrayList implementation code described earlier can process any object.

Indexer is a special type of attribute that can be added to a class to provide access similar to arrays. This keyword is used with parameters in square brackets, but it looks similar to other attributes. This syntax is reasonable because the object name will be used when accessing the index,

Followed by the index parameter in square brackets (for example, MyAnimals [0]).

Related Article

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.