26.3 |
Explain the use of the following notation in a C# program:
public class Array< E >
|
26.4 |
How can generic methods be overloaded?
|
26.5 |
The compiler performs a matching process to determine which method to call when a method is invoked. Under what circumstances does an attempt to make a match result in a compiletime error?
|
26.6 |
Explain why a C# program might use the statement
Array< Employee > workerlist = new Array< Employee >();
|
26.7 |
Write a generic method, Search, that implements the linear search algorithm. Method Search should compare the search key with each element in the array until the search key is found or until the end of the array is reached. If the search key is found, return its location in the array; otherwise, return -1. Write a test application that inputs and searches an int array and a double array. Provide buttons that the user can click to randomly generate int and double values. Display the generated values in a TextBox, so the user knows what values they can search for [Hint: Use (E : IComparable< E >) in the where clause for method Search so that you can use method CompareTo to compare the search key to the elements in the array.]
|
26.8 |
Overload generic method PrintArray of Fig. 26.3 so that it takes two additional int arguments: lowIndex and highIndex. A call to this method prints only the designated portion of the array. Validate lowIndex and highIndex. If either is out-of-range, or if highIndex is less than or equal to lowIndex, the overloaded PrintArray method should throw an InvalidIndexException; otherwise, PrintArray should return the number of elements printed. Then modify Main to exercise both versions of PrintArray on arrays intArray, doubleArray and charArray. Test all capabilities of both versions of PrintArray.
|
26.9 |
Overload generic method PrintArray of Fig. 26.3 with a non-generic version that prints an array of strings in neat, tabular format, as shown in the sample output that follows:
Array stringArray contains:
one two three four
five six seven eight
|
|
26.10 |
Write a simple generic version of method IsEqualTo that compares its two arguments with the Equals method, and returns TRue if they are equal and false otherwise. Use this generic method in a program that calls IsEqualTo with a variety of simple types, such as object or int. What result do you get when you attempt to run this program?
|
26.11 |
Write a generic class Pair which has two type parameters, F and S, representing the type of the first and second element of the pair, respectively. Add properties (with Get and Set accessors) for the first and second elements of the pair. [Hint: The class header should be public class Pair< F, S >.]
|
26.12 |
Convert classes treeNode and TRee from Figs. 25.19 and 25.20 into generic classes. To insert an object in a TRee, the object must be compared to the objects in existing TReeNodes. For this reason, classes TReeNode and tree should specify IComparable< E > as the interface constraint of each class's type parameter. After modifying classes TReeNode and TRee, write a test application that creates three tree objectsone that stores ints, one that stores doubles and one that stores strings. Insert 10 values into each tree. Then output the preorder, inorder and postorder traversals for each tree.
|
26.13 |
Modify your test program from Exercise 26.12 to use generic method TestTree to test the three TRee objects. The method should be called three timesonce for each TRee object.
|