- Creating Classes
- Creating Objects
- Using Access Modifiers
- Creating Fields and Using Initializers
- Creating Methods
- Creating Properties
- Read-only Properties
- Creating Constructors
- Creating Structs
- Creating Static Members
- Creating Static Fields
- Creating Static Properties
- Creating Destructors and Handling Garbage Collection
- Overloading Methods
- Overloading Operators
- Creating Namespaces
- In Brief
Overloading Methods
A major topic in OOP is overloading methods, which lets you define the same method multiple times so that you can call them with different argument lists (a method's argument list is called its signature). C# not only supports method overloading, but revels in it. Most of the methods you'll find built into C# have several overloaded forms to make life easier for youfor example, you can call System.Console.WriteLine with a string, a float, an int, and so on, all because it's been overloaded to handle those types of arguments (it has 18 overloaded forms in all).
It's easy to overload a method; just define it multiple times, each time with a unique signature. You can see an example in ch03_12.cs, Listing 3.12, where we're working with our Math2 class again, adding an overloaded method named Area that can return the area of both squares and rectangles.
You can call Area with either one or two arguments. If you call this method with one argument, a, it'll assume you want the area of a square whose sides are a long. If you call this method with two arguments, a and b, the method will assume you want the area of a rectangle which is a long and b high. In other words, the code can determine what method to call based on the methods' signatures.
Listing 3.12 Overloading a Method (ch03_12.cs)
class ch03_12 { static void Main() { System.Console.WriteLine("Here's the area of the square: {0}", Math2.Area(10)); System.Console.WriteLine("Here's the area of the rectangle: {0}", Math2.Area(10, 5)); } } public class Math2 { // This one's for squares public static double Area(double side) { return side * side; } // This one's for rectangles public static double Area(double length, double height) { return length * height; } }
Here's what you see when you run ch03_12.cs. As you can see, we've overloaded the method successfully:
C:\>ch03_12 Here's the area of the square: 100 Here's the area of the rectangle: 50
Overloading Constructors
You can overload constructors just like any other method. For example, we could give the Messager class two constructors. (The Messager class appears in Listing 3.2.) One of these will take a string argument which holds the text the Messager object's DisplayMessage method should display, and the other constructor takes no arguments and simply stores a default string for use with DisplayMessage. Here's what it looks like:
public Messager() { this.message = "Hello from C#."; } public Messager(string message) { this.message = message; }
You can see these two constructors at work in ch03_13.cs, Listing 3.13. In that code, we create Messager objects using both constructors, and then display the messages in those objects.
Listing 3.13 Overloading a Constructor (ch03_13.cs)
class ch03_13 { static void Main() { Messager obj1 = new Messager(); obj1.DisplayMessage(); Messager obj2 = new Messager("No worries."); obj2.DisplayMessage(); } } class Messager { public string message = "Default message."; public Messager() { this.message = "Hello from C#."; } public Messager(string message) { this.message = message; } public void DisplayMessage() { System.Console.WriteLine(message); } }
Here's what you see when you run ch03_13.cs. As you can see, we've used both constructors, the one that takes no parameters, and the one that takes a single string parameter:
C:\>ch03_13 Hello from C#. No worries.