Wrap-Up

Answers to Self Review Exercises

26.1

a) False. A generic method can be overloaded by non-generic methods with the same or a different number of arguments. b) False. All generic method declarations have a type parameter list that immediately follows the method's name. c) True. d) True. e) False. Type parameter names among different generic methods need not be unique. f) True. g) False. A type parameter can have at most one class constraint, but multiple interface constraints.

26.2

a) Generic methods, Generic classes. b) angle brackets. c) type parameters. d) ints. e) type parameter list. f) new.

Exercises

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.

Collections

Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Flylib.com © 2008-2020.
    If you may any questions please contact us: flylib@qtcs.net