Chapter 6 Methods 223 Common Programming Error 6.16 (Business web site)
Chapter 6 Methods 223 Common Programming Error 6.16 Accidentally having a nonrecursive method call itself through another method can cause infinite recursion. Most programming textbooks introduce recursion much later than we have done in this book. We feel that recursion is a sufficiently rich and complex topic that it is better to introduce it early and spread its examples over the remainder of the text. 6.17 Method Overloading C# enables several methods of the same name to be defined in the same class, as long as these methods have different sets of parameters (number of parameters, types of parameters or order of the parameters). This is called method overloading. When an overloaded method is called, the C# compiler selects the proper method by examining the number, types and order of the call s arguments. Method overloading commonly is used to create several methods with the same name that perform similar tasks, but on different data types. Figure 6.18 uses overloaded method Square to calculate the square of an int and a double. Good Programming Practice 6.6 Overloading methods that perform closely related tasks can make programs more readable and understandable. 1 // Fig. 6.18: MethodOverload.cs 2 // Using overloaded methods. 3 using System; 4 using System.Drawing; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 10 public class MethodOverload : System.Windows.Forms.Form 11 { 12 private System.Windows.Forms.Button showOutputButton; 13 private System.Windows.Forms.Label outputLabel; 14 15 // Visual Studio .NET generated code 16 17 // first version, takes one integer 18 public int Square ( int x ) 19 { 20 return x * x; 21 } 22 23 // second version, takes one double 24 public double Square ( double y ) 25 { 26 return y * y; 27 } 28 Fig. 6.18 Using overloaded methods. (Part 1 of 2.)
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.