Monday 17 December 2012

c sharp programming


THE SORTEDLIST CLASS

As we mentioned in the Introduction section of this chapter, a SortedList is a
data structure that stores key–value pairs in sorted order based on the key.We
can use this data structure when it is important for the keys to be sorted, such
as in a standard word dictionary, where we expect the words in the dictionary
to be sorted alphabetically. Later in the chapter, we’ll also see how the class
can be used to store a list of single, sorted values.

Using the SortedList Class
We can use the SortedList class in much the same way we used the classes
in the previous sections, since the SortedList class is a specialization of the
DictionaryBase class.
To demonstrate this, the following code creates a SortedList object that
contains three names and IP addresses:
SortedList myips = New SortedList();
myips.Add("Mike", "192.155.12.1");
myips.Add("David", "192.155.12.2");
myips.Add("Bernica", "192.155.12.3");

The name is the key and the IP address is the stored value.

The generic version of the SortedList class allows you to decide the data
type of both the key and the value:
SortedList<Tkey, TValue>
For this example, we could instantiate myips like this:
SortedList<string, string> myips =
new SortedList<string, string>();

A grade book sorted list might be instantiated as follows:
SortedList<string, int> gradeBook =
new SortedList<string, int>();
We can retrieve the values by using the Item method with a key as the
argument:

Foreach(Object key In myips.Keys)
Console.WriteLine("Name:"&key+"\n" +
"IP: " & myips.Item(key))
This fragment produces the following output:


Alternatively, we can also access this list by referencing the index num-
bers where these values (and keys) are stored internally in the arrays, which
actually store the data. Here’s how:
for(inti=0;i< myips.Count; i++)
Console.WriteLine("Name:"+ myips.GetKey(i) + "\n" +
"IP: " & myips.GetByIndex(i));


This code fragment produces the exact same sorted list of names and IP
addresses:



A key–value pair can be removed from a SortedList by either specifying a
key or specifying an index number, as in the following code fragment, which
demonstrates both removal methods:
myips.Remove("David");
myips.RemoveAt(1);

If you want to use index-based access into a SortedList but don’t know the
indexes where a particular key or value is stored, you can use the following
methods to determine those values:
int indexDavid = myips.GetIndexOfKey("David");
int indexIPDavid = _
myips.GetIndexOfValue(myips.Item("David"));
The SortedList class contains many other methods and you are encouraged
to explore them via VS.NET’s online documentation.

1 comment:

  1. Hi,

    I would suggest you to rename the posts to give some meaningful title and more specific titles. This will help seekers to reach to your blog more easily.

    For instance have a look at this technical blog:
    http://akashsoni7.blogspot.in

    Thanks,
    Ashish

    ReplyDelete