c# for dummies
Monday, 17 December 2012
c# for dummies: c sharp programming
c# for dummies: 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–valu...
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.
c# for dummies: c sharp programming
c# for dummies: c sharp programming: THE GENERIC KEYVALUEPAIR CLASS C# provides a small class that allows you to create dictionary-like objects that store data based on a key. T...
c sharp programming
THE GENERIC KEYVALUEPAIR CLASS
C# provides a small class that allows you to create dictionary-like objects that
store data based on a key. This class is called the KeyValuePair class. Each
object can only hold one key and one value, so its use is limited.
A KeyValuePair object is instantiated like this:
KeyValuePair<string, int> mcmillan =
new KeyValuePair<string, int>("McMillan", 99);
The key and the value are retrieved individually:
Console.Write(mcmillan.Key);
Console.Write(""+ mcmillan.Value);
The KeyValuePair class is better used if you put the objects in an array. The
following program demonstrates how a simple grade book might be imple-
mented:
using System;
using System.Collections.Generic;
using System.Text;
namespace Generics
{
class Program
{
static void Main(string[] args)
{
KeyValuePair<string, int>[] gradeBook = new
KeyValuePair<string, int>[10];
gradeBook[0] = new KeyValuePair<string,
int>("McMillan", 99);
gradeBook[1] = new KeyValuePair<string,
int>("Ruff", 64);
for (inti=0;i<= gradeBook.GetUpperBound(0); i++)
if (gradeBook[i].Value != 0)
Console.WriteLine(gradeBook[i].Key + ": " +
gradeBook[i].Value);
Console.Read();
}
}
}
C# provides a small class that allows you to create dictionary-like objects that
store data based on a key. This class is called the KeyValuePair class. Each
object can only hold one key and one value, so its use is limited.
A KeyValuePair object is instantiated like this:
KeyValuePair<string, int> mcmillan =
new KeyValuePair<string, int>("McMillan", 99);
The key and the value are retrieved individually:
Console.Write(mcmillan.Key);
Console.Write(""+ mcmillan.Value);
The KeyValuePair class is better used if you put the objects in an array. The
following program demonstrates how a simple grade book might be imple-
mented:
using System;
using System.Collections.Generic;
using System.Text;
namespace Generics
{
class Program
{
static void Main(string[] args)
{
KeyValuePair<string, int>[] gradeBook = new
KeyValuePair<string, int>[10];
gradeBook[0] = new KeyValuePair<string,
int>("McMillan", 99);
gradeBook[1] = new KeyValuePair<string,
int>("Ruff", 64);
for (inti=0;i<= gradeBook.GetUpperBound(0); i++)
if (gradeBook[i].Value != 0)
Console.WriteLine(gradeBook[i].Key + ": " +
gradeBook[i].Value);
Console.Read();
}
}
}
c# for dummies: c sharp programming
c# for dummies: c sharp programming: Other DictionaryBase Methods There are two other methods that are members of the DictionaryBase class: CopyTo and GetEnumerator. We dis...
c sharp programming
Other DictionaryBase Methods
There are two other methods that are members of the DictionaryBase class:
CopyTo and GetEnumerator. We discuss these methods in this section.
The CopyTo method copies the contents of a dictionary to a one-
dimensional array. The array should be declared as a DictionaryEntry array,
though you can declare it asObject and then use theCType function to convert
the objects to DictionaryEntry.
The following code fragment demonstrates howto use the CopyTomethod:
IPAddresses myIPs = new IPAddresses("c:\ips.txt");
DictionaryEntry[] ips = _
new DictionaryEntry[myIPs.Count-1];
myIPs.CopyTo(ips, 0);
The formula used to size the array takes the number of elements in the dic-
tionary and then subtracts one to account for a zero-based array. The CopyTo
method takes two arguments: the array to copy to and the index position to
start copying from. If you want to place the contents of a dictionary at the
end of an existing array, for example, you would specify the upper bound of
the array plus one as the second argument.
Once we get the data from the dictionary into an array, we want to work
with the contents of the array, or at least display the values. Here’s some code
to do that:
for(inti=0;i<= ips.GetUpperBound(0); i++)
Console.WriteLine(ips[i]);
Unfortunately, this is not what we want. The problem is that we’re storing
he data in the array as DictionaryEntry objects, and that’s exactly what we
see. If we use the ToString method:
Console.WriteLine(ips[ndex]ToString())
we get the same thing. In order to actually view the data in a DictionaryEntry
object,we have to use either the Key property or the Value property, depending
on if the object we’re querying holds key data or value data. So how do we
know which is which? When the contents of the dictionary are copied to the
array, the data is copied in key–value order. So the ?rst object is a key, the
second object is a value, the third object is a key, and so on.
Now we can write a code fragment that allows us to actually see the data:
for(inti=0;i<= ips.GetUpperBound(0); i++) {
Console.WriteLine(ips[index].Key);
Console.WriteLine(ips[index].Value);
}
c# for dummies: c sharp programming
c# for dummies: c sharp programming: Other DictionaryBase Methods There are two other methods that are members of the DictionaryBase class: CopyTo and GetEnumerator. We discuss ...
Subscribe to:
Posts (Atom)