use the example in page 69-70 to create a program that callls a class
user should input variables
program should
create an instance of an object for the following types of shapes
circle
square
rectangle
triangle
draw the shape
DC generic. Example 2.5 Problem: Create a program that includes two methods. One will be a regular method that can print an array of strings. Try to call that method to print an array of characters. It will fail. The other method will be generic and can print any type of strings. Analysis: A generic method header will look like this: accessSpecifier reuten Type methodName
In the body of the method, you must use the type parameter specified in the header (In this case "E"). The "E" is used as a placeholder. The specific type is given only when the method is called. 69 Agile Software Development with C# Book II The specific type takes the place of the "E" as the type parameter. Solution: Create a new project called Chapter 2 Generic Method with the following code inside the Main method: 1 using System; 2 namespace Chapter2GenericMethod 4 5 class Program { static void Main(string[] args) string[] astring - { "Alice", "Bob" }; char[] achar - { 'A', 'B' }; PrintMethod(aString); //PrintMethod(achar); PrintMethodGeneric(aString); PrintMethodGeneric char>(achar); Console.ReadKey(); // a method for print string array static void PrintMethod(string[] str) foreach(String s in str) Console.WriteLine(); 1/ a generic method for print anything static void PrintMethodGeneric(ED) a) foreach(Es in a) Console.WriteLine(s); Figure 2.15 Code for Example 2.5 DC generic. Example 2.5 Problem: Create a program that includes two methods. One will be a regular method that can print an array of strings. Try to call that method to print an array of characters. It will fail. The other method will be generic and can print any type of strings. Analysis: A generic method header will look like this: accessSpecifier reuten Type methodName In the body of the method, you must use the type parameter specified in the header (In this case "E"). The "E" is used as a placeholder. The specific type is given only when the method is called. 69 Agile Software Development with C# Book II The specific type takes the place of the "E" as the type parameter. Solution: Create a new project called Chapter 2 Generic Method with the following code inside the Main method: 1 using System; 2 namespace Chapter2GenericMethod 4 5 class Program { static void Main(string[] args) string[] astring - { "Alice", "Bob" }; char[] achar - { 'A', 'B' }; PrintMethod(aString); //PrintMethod(achar); PrintMethodGeneric(aString); PrintMethodGeneric char>(achar); Console.ReadKey(); // a method for print string array static void PrintMethod(string[] str) foreach(String s in str) Console.WriteLine(); 1/ a generic method for print anything static void PrintMethodGeneric(ED) a) foreach(Es in a) Console.WriteLine(s); Figure 2.15 Code for Example 2.5