Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Optional Parameters or Method Overload? [duplicate]

Tags:

Since C# added optional parameters is it considered a better practice to use optional parameters or method overloads or is there a particular case where you would want to use one over the other. i.e a function w/ lots of parameters would be better suited w/ optional parameters?

like image 540
Luke Belbina Avatar asked May 25 '11 23:05

Luke Belbina


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

Optional parameters are nice, but should be used when it makes sense. Optional parameters often muddy the intent of the method -- If there is another alternative I would lean towards the alternative.

Part of the need for optional parameters and named parameters is because COM allowed optional and name parameters:

MSDN

Some APIs, most notably COM interfaces such as the Office automation APIs, are written specifically with named and optional parameters in mind. Up until now it has been very painful to call into these APIs from C#, with sometimes as many as thirty arguments having to be explicitly passed, most of which have reasonable default values and could be omitted.

SomeNewKid from forums.asp.net puts succinctly:

http://forums.asp.net/t/386604.aspx/1

...overloaded methods are generally preferable to optional parameters. Why? To keep each of your methods clear in purpose. That is, each method should do one thing well. As soon as you introduce optional parameters, you are diluting the cleanliness of that method, and introducing branching logic that is probably best kept out of a method. This clarity of purpose becomes even more important when you start using inheritance. If you override a method that has one or more optional parameters, they become harder to work with. So, I'd suggest that for anything other than quick and dirty classes, you use overloading in preference to optional parameters.

Keep in mind that optional parameters are a syntactical sugar:

Reflector C#:

public class Class1 {     // Methods     public Class1()     {         this.Method1("3", "23");     }      public void Method1(string one, [Optional, DefaultParameterValue("23")] string two)     {     } } 

IL:

.class public auto ansi beforefieldinit Class1     extends [mscorlib]System.Object {     .method public hidebysig specialname rtspecialname instance void .ctor() cil managed     {         .maxstack 8         L_0000: ldarg.0          L_0001: call instance void [mscorlib]System.Object::.ctor()         L_0006: nop          L_0007: nop          L_0008: ldarg.0          L_0009: ldstr "3"         L_000e: ldstr "23"         L_0013: call instance void WebApplication1.Class1::Method1(string, string)         L_0018: nop          L_0019: nop          L_001a: ret      }      .method public hidebysig instance void Method1(string one, [opt] string two) cil managed     {         .param [2] = string('23')         .maxstack 8         L_0000: nop          L_0001: ret      }  } 
like image 198
Chuck Conway Avatar answered Sep 21 '22 15:09

Chuck Conway