Professional ASP.NET 1.1 [Electronic resources]

Alex Homeret

نسخه متنی -صفحه : 244/ 122
نمايش فراداده

System.Collections.Specialized Namespace

The

System.Collections.Specialized namespace contains collection classes that are suited to specialized tasks, as well as collection classes that are strongly typed, which, although it limits the types that can be contained, reduces the amount of casting needed.

The StringCollection Class

The

System.Collections.Specialized.StringCollection class provides an implementation of an

ArrayList that can only contain string values. This class implements all of the functionality of

ArrayList , but does all of the casting behind the scenes. Like

ArrayList ,

StringCollection implements the

IList interface.

Here is a simple example of using the

StringCollection class in C#:

StringCollection names = new StringCollection();

names.Add("Richard");

names.Add("Sam");

names.Add("Richard");

names.Add("Sam");

foreach (String name in names)

{

Response.Write("<br />" + name);

}

The StringDictionary Class

The

System.Collections.Specialized.StringDictionary class provides an implementation of a hash table, in which the key and value are always of type

System.String . The key is also case- insensitive. Internally, this class uses the

Hashtable class for its implementation. If you're interested only in dealing with string values, you should use this class.

The following C# code shows how to use

StringDictionary to locate the surname of a given person:

StringDictionary names;

string surname;

names = new StringDictionary();

names["Richard"] = "Anderson";

names["Alex"] = "Homer";

surname = names["Alex"];

Response.Write("The surname for alex is " + surname);