eBabel Blog

Make a website

70-536 exam: StringCollection class

12 March 2010

Following on my blog post about Hashtables, for the .NET fundamentals exam you should be familiar with Collections and Specialised Collections like the StringCollection Class (which represents a collection of strings).

Namespace: System.Collections.Specialized
Assembly: System.dll
C#
[SerializableAttribute]
public class StringCollection : IList, ICollection, IEnumerable

This class implements the IList interface, lists are a common data structure in computer science:

In computer science, a list or sequence is an abstract data structure that implements an ordered collection of values, where the same value may occur more than once. An instance of a list is a computer representation of the mathematical concept of a finite sequence, that is, a tuple. Each instance of a value in the list is usually called an item, entry, or element of the list; if the same value occurs multiple times, each occurrence is considered a distinct item.

Source: Wikipedia

Singly linked list
A singly-linked list structure, implementing a list with 3 integer elements.

Source: Wikipedia

The name list is also used for several concrete data structures that can be used to implement abstract lists, especially linked lists.
The so-called static list structures allow only inspection and enumeration of the values. A mutable or dynamic list may allow items to be inserted, replaced, or deleted during the list's existence

Source: Wikipedia

For exam 70-536 you should be comfortable with basic code examples to create, populate and manipulate StringCollections:

StringCollection myCol = new StringCollection();
String[] myArr = new String[] { "red", "green", "blue" };
myCol.AddRange( myArr );
foreach (Object obj in myCol) Console.WriteLine("{0}", obj);
Console.WriteLine();

in the Console this returns:

red
green
blue

Next we can use the CopyTo method to convert our StringCollection into a String Array

String[] myArr2 = new String[myCol.Count];
myCol.CopyTo(myArr2, 0);
for (i = 0; i < myArr2.Length; i++)
{
Console.WriteLine("[{0}] {1}", i, myArr2[i]);
}
Console.WriteLine();

this returns:

[0] red
[1] green
[2] blue

Lastly the following shows the Add (at the end of the list) and Insert (after “position n” in list)

myCol.Add("white");
myCol.Insert(2, "gray");
foreach (Object obj in myCol) Console.WriteLine("{0}", obj);
Console.WriteLine();

this returns:

red
green
gray
blue
white

For more examples, check out MSDN sample code

If you liked this blog post, you may want to subscribe to my news feed in your RSS reader.