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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Apr 1, 2011, 2:49 AM
madhav tarePosted Apr 1, 2011, 2:25 AM
But I am looking for only optional parameter..& not for verloading
madhav tarePosted Apr 1, 2011, 2:24 AM
Sam HobbsPosted Apr 1, 2011, 1:39 AM
VulpesPosted Mar 31, 2011, 9:38 AM
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.
Guest UserPosted Mar 31, 2011, 9:24 AM
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
.
.
.
}
VulpesPosted Mar 31, 2011, 9:13 AM
See the article on this topic in the sidebar to the right of this thread.