Strongly Typed Collection ArrayList and Array

1.Try using arrays instead
Arrays will generally be more efficient in terms of access speed and memory usage than any collection and in many cases will be all you need. 
In most cases, arrays are superior to collections. The only exceptions to this rule is when:
  • ·        The number of objects can shrink or grow unpredictably.
  • ·        The objects you’ll be storing aren’t of all the same type.
  • ·        You need to perform some processing on the object when it is added to or retrieved from the array.

2.So you really do need a collection?
If you’ve decided that you definitely need a collection, you need to determine what sort of collection best fits the situation: a collection, a formal list, or a dictionary. Briefly, these collection types break down like this:

  • ·A collection is simply an ordered group of objects that are retrievable only by enumerating through the sort order. Check out the System.Collections.ICollection interface for a formal definition of a collection.
  • ·A formal list, on the other hand, allows indexing directly into the underlying collection and also supports enumeration. Formal lists are defined by the System.Collections.IList interface.
  • ·The dictionary, like a formal list, allows both enumeration and indexing directly into the collection but stores each object along with an associated key value and sorts the objects by those key values. See System.Collections.IDictionary for more on this. 

3 Using strongly typed collection other than arraylist.

 1 
 2 using System;
 3 using System.Collections;
 4 using System.Collections.Generic;
 5 
 6 namespace LN.HP.CSharpBasic
 7 {
 8 /// <summary>
 9 /// Using Strongly Typed Collections.
10 /// </summary>
11     public class SamplesArrayList
12     {
13 
14         public static void Main()
15         {
16 
17             #region 1
18             // Creates and initializes a new ArrayList.
19             ArrayList myAL = new ArrayList();
20             myAL.Add("Hello");
21             myAL.Add(new Person("Toby"12));
22             myAL.Add("!");
23 
24             // Displays the properties and values of the ArrayList.
25             Console.WriteLine("myAL");
26             Console.WriteLine("    Count:    {0}", myAL.Count);
27             #endregion 1
28 
29             #region 2
30             PeopleCollection myPeople = new PeopleCollection();
31             myPeople.AddPerson(new Person("Homer"40));
32             myPeople.AddPerson(new Person("Marge"38));
33 
34             // This would be a compile-time error!
35             // myPeople.AddPerson(new object());
36 
37             foreach (Person p in myPeople)
38             {
39                 Console.WriteLine(p);
40             }
41             #endregion 2
42 
43             #region 3
44             List<string> mylist = new List<string>();
45 
46             mylist.Add("1");
47             mylist.Add("2");
48 
49             //build error.
50             //mylist.Add(3);
51             #endregion 3
52 
53         }
54 
55     }
56 
57     public class Person
58     {
59 
60         private int age;
61         private string name;
62 
63         public Person(string name, int age)
64         {
65             this.name = name;
66             this.age = age;
67         }
68     }
69 
70     public sealed class PeopleCollection : IEnumerable
71     {
72         private ArrayList arPeople = new ArrayList();
73         public PeopleCollection() { }
74 
75         // Cast for caller.
76         public Person GetPerson(int pos)
77         { return (Person)arPeople[pos]; }
78 
79         // Only insert Person types.
80         public void AddPerson(Person person)
81         { arPeople.Add(person); }
82 
83         public void ClearPeople()
84         { arPeople.Clear(); }
85 
86         public int Count
87         { get { return arPeople.Count; } }
88 
89         // Foreach enumeration support.
90         IEnumerator IEnumerable.GetEnumerator()
91         { return arPeople.GetEnumerator(); }
92     }
93 
94 }
95 

 Extension Reading: http://msdn.microsoft.com/en-us/library/41107z8a.aspx

posted on 2010-04-28 17:16  刘宁Toby  阅读(273)  评论(0编辑  收藏  举报