tailieunhanh - Microsoft Visual C# 2010 Step by Step (P9)

Tham khảo sách 'microsoft visual c# 2010 step by step (p9)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 370 Part III Creating Components 9. Press the Enter key to return to Visual Studio 2010. 10. Add the following statements shown in bold type to the end of the Main method in the Program class after the existing code static void Main string args Tree string tree2 new Tree string Hello World How Are You Today I Hope You Are Feeling Well These statements create another binary tree for holding strings populate it with some test data and then print the tree. This time the data is sorted alphabetically. 11. On the Build menu click Build Solution. Verify that the solution compiles and correct any errors if necessary. 12. On the Debug menu click Start Without Debugging. The program runs and displays the integer values as before followed by the strings in the following sequence Are Are Feeling Hello Hope How I Today Well World You You 13. Press the Enter key to return to Visual Studio 2010. Creating a Generic Method As well as defining generic classes you can also use the .NET Framework to create generic methods. With a generic method you can specify parameters and the return type by using a type parameter in a manner similar to that used when defining a generic class. In this way you can define generalized methods that are type-safe and avoid the overhead of casting and boxing in some cases . Generic methods are frequently used in conjunction with generic classes you need them for methods that take a generic class as a parameter or that have a return type that is a generic class. Chapter 18 Introducing Generics 371 You define generic methods by using the same type parameter syntax that you use when creating generic classes. You can also specify constraints. For example you can call the following generic Swap T method to swap the values in its parameters. Because this functionality is useful regardless of