Title: Use overloaded methods to simulate optional parameters in C#
This example shows how you can simulate optional parameters if you're using an older version of C#. Named and optional parameters were introduced in C# 2010. If you're using an older version, you should probably upgrade if you can. But sometimes you may be stuck using an older version. I have a couple of clients that use C# 2008, so their programs can't use optional parameters. (I also generally post examples in C# 2008 because it's easier for you to upgrade a project to a newer version than it is to downgrade an example to an older version.)
Anyway, you can sometimes use overloaded versions of a method to achieve the same results as an optional parameter. To overload a method, make multiple methods with the same name but different signatures. The methods' parameter lists must be different in data type or in number so C# can tell which one you want to invoke based on the parameters that you pass into a method call.
The following code shows three overloaded versions of the DrawDiamond method.
// Draw a diamond, optionally filling it.
private void DrawDiamond(Graphics gr, RectangleF bounds)
{
DrawDiamond(gr, bounds, Pens.Black);
}
private void DrawDiamond(Graphics gr, RectangleF bounds,
Pen diamond_pen)
{
DrawDiamond(gr, bounds, diamond_pen, Brushes.Transparent);
}
private void DrawDiamond(Graphics gr, RectangleF bounds,
Pen diamond_pen, Brush diamond_brush)
{
// Make the diamond's points.
PointF[] points = new PointF[4];
float xmid = (bounds.Left + bounds.Right) / 2;
float ymid = (bounds.Top + bounds.Bottom) / 2;
points[0] = new PointF(xmid, bounds.Top);
points[1] = new PointF(bounds.Right, ymid);
points[2] = new PointF(xmid, bounds.Bottom);
points[3] = new PointF(bounds.Left, ymid);
// Fill the diamond.
gr.FillPolygon(diamond_brush, points);
// Draw the diamond.
gr.DrawPolygon(diamond_pen, points);
}
The first version of DrawDiamond invokes the second and the second invokes the third. The third version does all of the real work. The first two methods are basically versions with missing parameters, simulating optional parameters.
The example program uses the following Paint event handler to draw two diamonds, one with every parameter specified and one with the Pen and Brush parameters omitted.
// Draw two diamonds.
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
// Draw diamonds on the form.
RectangleF rect1 = new RectangleF(4, 4,
this.ClientSize.Width / 2 - 6,
this.ClientSize.Height - 8);
RectangleF rect2 = new RectangleF(
rect1.Right + 4, 4,
this.ClientSize.Width / 2 - 6,
this.ClientSize.Height - 8);
DrawDiamond(e.Graphics, rect1, Pens.Red, Brushes.Yellow);
DrawDiamond(e.Graphics, rect2);
}
The program also defines (but doesn't demonstrate) the following version of DrawDiamond, which omits the Pen and takes a Brush parameter instead.
private void DrawDiamond(Graphics gr,
RectangleF bounds, Brush diamond_brush)
{
DrawDiamond(gr, bounds, Pens.Transparent, diamond_brush);
}
This doesn't really look like the preceding method with optional parameters, but it may be convenient. It's allowed because it's parameter list is different in type from the parameter lists used by all of the other versions.
Download the example to experiment with it and to see additional details.
|