Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Default value

Status
Not open for further replies.

Morpheus1981

Programmer
Aug 9, 2001
105
CR
Is it posible to declare a function parameter with a default value in C#?
 
You can't do this in c#.
I saw an example in msdn in the url:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/cpguide/html/cpconaccessingdefaultargumentvalues.htm
there you can see that you can declare such a function only in VB, but you can call it in c#.

Another way to implement this is overload.

Suppose you have this function in c++:
void fun(int a, int b=20);
you can call it in c++:
fun(10);
and have: a=10, b=20;

I suggest to do in c#:
void fun(int a, int b)
{
...
}
void fun(int a)
{
fun(a,20); // this will cal the first version of fun with b=20
}
 
Now I found a help page that suggest overloading and demonstrate this.
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/cscon/html/vctskcodesimulatingdefaultparametersvisualc.htm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top