By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

Optional parameter declaration and default values

P: n/a
Hello All,
I am new to C# , how can I delare parameters to a function as optional,
and also how are default values assigned ?

consider the function

public String myFunc(String thisisOptional, String OptWithDefaultValue)
{

}

Imran.
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Imran Aziz" <im***@tb2.net> wrote in message
news:uu*************@TK2MSFTNGP09.phx.gbl...
I am new to C# , how can I delare parameters to a function as optional,


You don't - you use overloads.

public string myFunc()
{
/*
do something
*/
}

public string myFunc(string thisisOptional)
{
/*
do something with thisisOptional
*/
string newString = myFunc();
}
Nov 17 '05 #2

P: n/a
I see, it means there is no way to declare optional function parameters in
C#, strange , no doubt I was not able to find it in the documentation :)

Thanks a lot.

Imran.
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"Imran Aziz" <im***@tb2.net> wrote in message
news:uu*************@TK2MSFTNGP09.phx.gbl...
I am new to C# , how can I delare parameters to a function as optional,


You don't - you use overloads.

public string myFunc()
{
/*
do something
*/
}

public string myFunc(string thisisOptional)
{
/*
do something with thisisOptional
*/
string newString = myFunc();
}

Nov 17 '05 #3

P: n/a
Hi,

In this video Anders Hejlsberg, the designer of C#, explains why they
decided to leave optional parameters out of C#:

http://msdn.microsoft.com/msdntv/epi...h/manifest.xml

Regards - Octavio

"Imran Aziz" <im***@tb2.net> escribió en el mensaje
news:%2***************@TK2MSFTNGP15.phx.gbl...
I see, it means there is no way to declare optional function parameters in
C#, strange , no doubt I was not able to find it in the documentation :)

Thanks a lot.

Imran.
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"Imran Aziz" <im***@tb2.net> wrote in message
news:uu*************@TK2MSFTNGP09.phx.gbl...
I am new to C# , how can I delare parameters to a function as optional,


You don't - you use overloads.

public string myFunc()
{
/*
do something
*/
}

public string myFunc(string thisisOptional)
{
/*
do something with thisisOptional
*/
string newString = myFunc();
}


Nov 17 '05 #4

P: n/a
"Imran Aziz" <im***@tb2.net> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
it means there is no way to declare optional function parameters in C#
Correct.
strange


Nope - entirely by design.
Nov 17 '05 #5

P: n/a
You can't.

Use:
1) Overloads
public String myFunc()
{
myFunc(null, x //default value);
}

public String myFunc(String thisisOptional)
{
myFunc(thisisOptional, x //default value);
}

public String myFunc(String thisisOptional, String OptWithDefaultValue)
{
..... code here.
}

2) params object[] parameters like parameter in the method.

public String myFunc(params string[] parameters )
{
String thisisOptional = null;
String OptWithDefaultValue = defaultValue;

if (params.Lenght >= 2)
{
thisisOptional = parameters[0];
OptWithDefaultValue = parameters[1];
}
else if (params.Lenght == 1)
thisisOptional = parameters[0];

.... code here
}

Gustavo

"Imran Aziz" <im***@tb2.net> wrote in message
news:uu*************@TK2MSFTNGP09.phx.gbl...
Hello All,
I am new to C# , how can I delare parameters to a function as optional,
and also how are default values assigned ?

consider the function

public String myFunc(String thisisOptional, String OptWithDefaultValue)
{

}

Imran.

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.