Hi...All,
Could u please tell me dat
Is there optional parameters in c# & how to use dat ??
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Mar 31, 2011, 5:13 AM EDT
See the article on this topic in the sidebar to the right of this thread.
Guest UserRank #1,422 · 1.2K reputationPosted Mar 31, 2011, 5:24 AM EDT
void Foo()
{
Foo(null, null);
}
void Foo(int param1)
{
Foo(param1, null);
}
void Foo(int param1, string param2)
{
// determine if param1 and/or param2 are null
.
.
.
}
VulpesRank #55 · 29.2K reputationPosted Mar 31, 2011, 5:38 AM EDT
This is because:
1. Optional parameters are not CLS compliant - there may be other .NET languages that don't support them even though the main ones (VB.Net, C++/CLI) do.
2. Using optional parameters bakes the default value into the method's signature and so, if you change it later, clients need to be recompiled. This isn't the case with John's method because, of course, the default value doesn't appear in the method's signature.
The second of these is thought to be the reason why optional parameters were omitted from C# until version 4.0 but this objection was then overcome by the need to make Office interop easier. Previously it had been a PITA with the need to use Type.Missing for each optional argument in an Office interop method which you were not supplying explicitly.
A related topic to optional parameters is named parameters for which there's also an article in the sidebar.
Sam HobbsRank #107 · 20.5K reputationPosted Mar 31, 2011, 9:39 PM EDT
madhav tare9 reputationPosted Mar 31, 2011, 10:24 PM EDT
madhav tare9 reputationPosted Mar 31, 2011, 10:25 PM EDT
But I am looking for only optional parameter..& not for verloading
Sam HobbsRank #107 · 20.5K reputationPosted Mar 31, 2011, 10:49 PM EDT