For example, if you called DoSomething(0, “”), num would be set to 1 and str would be set to “optional” even though you didn’t want that to occur.

]]>By: anonhttps://www.sitepoint.com/c-sharp-net-optional-parameters/#comment-70862
Thu, 20 May 2010 02:48:13 +0000http://www.sitepoint.com/blogs/?p=21437#comment-70862in javascript you can check if a parameter is undefined using the || operator, ie:

]]>By: logic_earthhttps://www.sitepoint.com/c-sharp-net-optional-parameters/#comment-70861
Wed, 19 May 2010 07:09:25 +0000http://www.sitepoint.com/blogs/?p=21437#comment-70861Not when you have Named Parameters which C# 4.0 also introduces.
]]>By: Timhttps://www.sitepoint.com/c-sharp-net-optional-parameters/#comment-70860
Tue, 18 May 2010 23:34:42 +0000http://www.sitepoint.com/blogs/?p=21437#comment-70860The major difference is that with optional parameters, you have to keep them all in the same order. Overloading allows you to switch the order of the parameters around. If you want to leave optional parameters out when calling a function/method, you have to left them out from right to left.
]]>By: key_bhttps://www.sitepoint.com/c-sharp-net-optional-parameters/#comment-70859
Tue, 18 May 2010 16:19:51 +0000http://www.sitepoint.com/blogs/?p=21437#comment-70859I just use Pytho ;)
]]>By: Anonymoushttps://www.sitepoint.com/c-sharp-net-optional-parameters/#comment-70858
Tue, 18 May 2010 15:27:53 +0000http://www.sitepoint.com/blogs/?p=21437#comment-70858Hi Craig,
In your last example “earlier versions of C#”, no need to duplicate all your code. Simply call the must complete function.
// no parameters
void DoSomething()
{
int num = 1;
string str = “optional”;
DoSomething(num, str);
}
// one parameter
void DoSomething(int num)
{
string str = “optional”;
DoSomething(num, str);
}
]]>By: SlitheryImphttps://www.sitepoint.com/c-sharp-net-optional-parameters/#comment-70857
Tue, 18 May 2010 14:13:03 +0000http://www.sitepoint.com/blogs/?p=21437#comment-70857Actually, you do not have to keep repeating the // code block when using overloads, just call the ‘full’ overload with the optional parameters:
// no parameters
void DoSomething()
{
DoSomething(1, “optional”);
}
//
// one parameter
void DoSomething(int num)
{
DoSomething(num, “optional”);
}
//
// two parameters
void DoSomething(int num, string str)
{
// code
}
]]>