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

Methods, Named Parameters

P: n/a
Hi Guys,

Can I use named parameters in C# Methods. i.e doFoo(fname="do",
lname="Foo");

Cheers, Pete
Nov 16 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
Peter,

Those arent named parameters (all parameters have a name, no?). Rather,
you would create overloads, like this:

public void doFoo()
{
// Call the overload.
doFoo("do");
}

public void doFoo(string fname)
{
// Call the overload
doFoo(fname, "Foo");
}

public void doFoo(string fname, string lname)
{
// Do something here.
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Hardy" <pe**@nospam.com> wrote in message
news:Oh*************@TK2MSFTNGP10.phx.gbl...
Hi Guys,

Can I use named parameters in C# Methods. i.e doFoo(fname="do",
lname="Foo");

Cheers, Pete

Nov 16 '05 #2

P: n/a
Hi Nicholas,

In VB and in SQL, these are called named parameters.

Peter: I do not believe that this construct is a feature of the C# language.
This would require that all parameters that you didn't provide would have a
default value assigned to them, and C# provides no simple way for a
developer to indicate the default values that should be assigned to the
missing parameters. You can pass Type.Missing if you wish. However, the
more common approach is to do what Nicholas suggests: overload the method
signature with a new signature containing fewer parameters. However, if you
want to support combinations that involve multiple parameters of the same
type... you can't.

VB.NET does not have this problem. It has support for named parameters. Of
course, SQL does too.

References:
http://dotnetjunkies.com/WebLog/pete...0/22/2778.aspx
http://msdn.microsoft.com/library/de...parameters.asp

HTH,
--- Nick

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eh**************@TK2MSFTNGP15.phx.gbl...
Peter,

Those arent named parameters (all parameters have a name, no?). Rather, you would create overloads, like this:

public void doFoo()
{
// Call the overload.
doFoo("do");
}

public void doFoo(string fname)
{
// Call the overload
doFoo(fname, "Foo");
}

public void doFoo(string fname, string lname)
{
// Do something here.
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Hardy" <pe**@nospam.com> wrote in message
news:Oh*************@TK2MSFTNGP10.phx.gbl...
Hi Guys,

Can I use named parameters in C# Methods. i.e doFoo(fname="do",
lname="Foo");

Cheers, Pete


Nov 16 '05 #3

P: n/a
Hi Peter,

Nop, C# does not support named parameters, only "positional" parameters.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Peter Hardy" <pe**@nospam.com> wrote in message
news:Oh*************@TK2MSFTNGP10.phx.gbl...
Hi Guys,

Can I use named parameters in C# Methods. i.e doFoo(fname="do",
lname="Foo");

Cheers, Pete

Nov 16 '05 #4

P: n/a
Hi,
I think that what he is refering is to have the opportunity to change the
order of the parameters and that the compiler reorder them based on the name
Ex::
void Method( string firstName, string lastName){}

You could do this
Method( lastName="xxx" , firstName="...." )

and the compiler would reorder the parameters at compiling time.

I know that some language support this, but I cannot remember which one

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eh**************@TK2MSFTNGP15.phx.gbl...
Peter,

Those arent named parameters (all parameters have a name, no?).
Rather, you would create overloads, like this:

public void doFoo()
{
// Call the overload.
doFoo("do");
}

public void doFoo(string fname)
{
// Call the overload
doFoo(fname, "Foo");
}

public void doFoo(string fname, string lname)
{
// Do something here.
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Hardy" <pe**@nospam.com> wrote in message
news:Oh*************@TK2MSFTNGP10.phx.gbl...
Hi Guys,

Can I use named parameters in C# Methods. i.e doFoo(fname="do",
lname="Foo");

Cheers, Pete


Nov 16 '05 #5

P: n/a
Nick,

Named parameters is a foolish name, since they all have a name =)

Anyways, you can't pass Type.Missing unless the parameter is of type
object (and you shouldn't be declaring these parameters as type object if
they are really only a type of something else), because it is of type object
(or rather, returned as type object).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:csWxd.267093$HA.8181@attbi_s01...
Hi Nicholas,

In VB and in SQL, these are called named parameters.

Peter: I do not believe that this construct is a feature of the C#
language.
This would require that all parameters that you didn't provide would have
a
default value assigned to them, and C# provides no simple way for a
developer to indicate the default values that should be assigned to the
missing parameters. You can pass Type.Missing if you wish. However, the
more common approach is to do what Nicholas suggests: overload the method
signature with a new signature containing fewer parameters. However, if
you
want to support combinations that involve multiple parameters of the same
type... you can't.

VB.NET does not have this problem. It has support for named parameters.
Of
course, SQL does too.

References:
http://dotnetjunkies.com/WebLog/pete...0/22/2778.aspx
http://msdn.microsoft.com/library/de...parameters.asp

HTH,
--- Nick

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:eh**************@TK2MSFTNGP15.phx.gbl...
Peter,

Those arent named parameters (all parameters have a name, no?).

Rather,
you would create overloads, like this:

public void doFoo()
{
// Call the overload.
doFoo("do");
}

public void doFoo(string fname)
{
// Call the overload
doFoo(fname, "Foo");
}

public void doFoo(string fname, string lname)
{
// Do something here.
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Hardy" <pe**@nospam.com> wrote in message
news:Oh*************@TK2MSFTNGP10.phx.gbl...
> Hi Guys,
>
> Can I use named parameters in C# Methods. i.e doFoo(fname="do",
> lname="Foo");
>
> Cheers, Pete
>
>



Nov 16 '05 #6

P: n/a
Hi Guyz,

Thanks for the feedback. I find named parameters (hey, I don't choose the
lingo) to be a useful way to document code.

For example, calling

ticket = new FormsAuthenticationTicket
(
version = 1,
name = txtUserName.Text,
issueDate = DateTime.Now,
expiration = DateTime.Now.AddMinutes(30),
isPersistant = false,
userData = "Users",
cookiePath = FormsAuthentication.FormsCookiePath
);

says a lot more to anyone reading the code than

ticket = new FormsAuthenticationTicket
(
1,
txtUserName.Text,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
"Users",
FormsAuthentication.FormsCookiePath
);

I have this sinking feeling I'm gonna have to learn Visual Basic now ... And
just before Christmas too :(

Cheers Pete

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:Og**************@TK2MSFTNGP09.phx.gbl...
Hi Peter,

Nop, C# does not support named parameters, only "positional" parameters.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Peter Hardy" <pe**@nospam.com> wrote in message
news:Oh*************@TK2MSFTNGP10.phx.gbl...
Hi Guys,

Can I use named parameters in C# Methods. i.e doFoo(fname="do",
lname="Foo");

Cheers, Pete


Nov 16 '05 #7

P: n/a

"Peter Hardy" <pe**@nospam.com> wrote in message
news:Oj****************@TK2MSFTNGP12.phx.gbl...
Hi Guyz,

Thanks for the feedback. I find named parameters (hey, I don't choose the
lingo) to be a useful way to document code.

For example, calling

ticket = new FormsAuthenticationTicket
(
version = 1,
name = txtUserName.Text,
issueDate = DateTime.Now,
expiration = DateTime.Now.AddMinutes(30),
isPersistant = false,
userData = "Users",
cookiePath = FormsAuthentication.FormsCookiePath
);

says a lot more to anyone reading the code than

ticket = new FormsAuthenticationTicket
(
1,
txtUserName.Text,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
"Users",
FormsAuthentication.FormsCookiePath
);

I have this sinking feeling I'm gonna have to learn Visual Basic now ...
And
just before Christmas too :(


Out of curiosity, do you have any other reasons for liking them? The IDE
really kills off any need to use named parameters in this case, IMHO.
Nov 16 '05 #8

P: n/a
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:

<snip>
Out of curiosity, do you have any other reasons for liking them? The IDE
really kills off any need to use named parameters in this case, IMHO.


Alternatively, for actually just reading code, it would be better not
to use a constructor which takes hundreds of parameters - instead, use
properties. If the properties shouldn't be changed after construction,
create a separate class encapsulating the "configuration", and pass
that in as an argument to the constructor.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

P: n/a
Sure, check out COM interop. You'll soon see the problems that come from not
having named / default parameters. (Hint: I don't like typing!)

Cheers, Pete

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:Oj**************@TK2MSFTNGP11.phx.gbl...

"Peter Hardy" <pe**@nospam.com> wrote in message
news:Oj****************@TK2MSFTNGP12.phx.gbl...
Hi Guyz,

Thanks for the feedback. I find named parameters (hey, I don't choose the lingo) to be a useful way to document code.

For example, calling

ticket = new FormsAuthenticationTicket
(
version = 1,
name = txtUserName.Text,
issueDate = DateTime.Now,
expiration = DateTime.Now.AddMinutes(30),
isPersistant = false,
userData = "Users",
cookiePath = FormsAuthentication.FormsCookiePath
);

says a lot more to anyone reading the code than

ticket = new FormsAuthenticationTicket
(
1,
txtUserName.Text,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
"Users",
FormsAuthentication.FormsCookiePath
);

I have this sinking feeling I'm gonna have to learn Visual Basic now ...
And
just before Christmas too :(


Out of curiosity, do you have any other reasons for liking them? The IDE
really kills off any need to use named parameters in this case, IMHO.

Nov 16 '05 #10

P: n/a
If I follow your example my code will look as follows:

ticket = new FormsAuthenticationTicket();
ticket.Version = 1;
ticket.Name = txtUserName.Text;
ticket.IsPersistent = false;
ticket.IssueDate = DateTime.Now;
ticket.Expiration = DateTime.Now.AddMinutes(30);
ticket.UserData = "Users";
ticket.CookiePath = FormsAuthentication.FormsCookiePath;

Unfortunately, FormsAuthenticationTicket (and I suspect many other .Net
classes) doesn't provide and empty constructor.

This means I have to write the code as follows:

// param1 = name, param2 = isPersistant, param3 = duration
ticket = new FormsAuthenticationTicket(txtUserName.Text, false, 30);
ticket.Version = 1;
ticket.IssueDate = DateTime.Now;
ticket.UserData = "Users";
ticket.CookiePath = FormsAuthentication.FormsCookiePath;

Its not great but hey, I can read it on the train without needing access to
the api. But there's still a problem - it doesn't work. Once the class has
been created, its properties are read only which mean I can't set them. This
leaves me with two options 1) Provide a comment before the constructor
listing parameters or 2) create a seperate and sensibly named variable for
each parameter and provide them to the constructor. You can see the
disadvantages with both approaches. If anyone can think of a nicer way to
write the aboce code so its a little cleared I'd love to see it.

I know I must seem like a pedant but named parameters are available in
SmallTalk and Visual Basic but not in C# and I can't for the life of me
think why.

Cheers, Pete


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:

<snip>
Out of curiosity, do you have any other reasons for liking them? The IDE
really kills off any need to use named parameters in this case, IMHO.


Alternatively, for actually just reading code, it would be better not
to use a constructor which takes hundreds of parameters - instead, use
properties. If the properties shouldn't be changed after construction,
create a separate class encapsulating the "configuration", and pass
that in as an argument to the constructor.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #11

P: n/a

"Peter Hardy" <pe**@nospam.com> wrote in message
news:e%****************@tk2msftngp13.phx.gbl...
Sure, check out COM interop. You'll soon see the problems that come from
not
having named / default parameters. (Hint: I don't like typing!)


Named parameters is highly seperate from default\optional parameters. While
one may add features to the other, you would be quite capable of using named
parameters without the horrific default\optional parameter issue(just think,
COM would have been so nice if they had just left that idea out).
Nov 16 '05 #12

P: n/a
>
I know I must seem like a pedant but named parameters are available in
SmallTalk and Visual Basic but not in C# and I can't for the life of me
think why.


The simple answer? Its not syntactially simple.

lname="Foo" is a valid assignment expression, it resolves to "Foo". If there
is a local with the name "lname" and a parameter with the name "lname", how
is the compiler to determine which you actually mean? The compiler could
guess, but that would be rather unsatisfactory, I would think.

The only feasible way to support it would be to add special syntax, however,
that would break consistency in the langauge, especially since the named
parameter like property setter syntax in attributes uses the name=value
method, although that is syntactically unambigious, unlike normal method
calls.

Nov 16 '05 #13

P: n/a
Wouldn't the complier know that lname=foo was a named parameter by virtue of
the fact that it's included in the method call? and the compiler would now
in advance that lname is a valid parameter name from the signature of the
method being called?

I hope you don't mind me following this thread, its just I suspect there is
a gap in my understanding and I'd like to fill it.

Cheers, Pete

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:#o**************@TK2MSFTNGP12.phx.gbl...

I know I must seem like a pedant but named parameters are available in
SmallTalk and Visual Basic but not in C# and I can't for the life of me
think why.
The simple answer? Its not syntactially simple.

lname="Foo" is a valid assignment expression, it resolves to "Foo". If

there is a local with the name "lname" and a parameter with the name "lname", how is the compiler to determine which you actually mean? The compiler could
guess, but that would be rather unsatisfactory, I would think.

The only feasible way to support it would be to add special syntax, however, that would break consistency in the langauge, especially since the named
parameter like property setter syntax in attributes uses the name=value
method, although that is syntactically unambigious, unlike normal method
calls.

Nov 16 '05 #14

P: n/a
Please ignore my last message, after a bit of thinking (and a mince pie) I
understand what your saying.

Forgive me, I'm in mourning for a feature that was never even there <grin>

Cheers, Pete

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:#o**************@TK2MSFTNGP12.phx.gbl...

I know I must seem like a pedant but named parameters are available in
SmallTalk and Visual Basic but not in C# and I can't for the life of me
think why.
The simple answer? Its not syntactially simple.

lname="Foo" is a valid assignment expression, it resolves to "Foo". If

there is a local with the name "lname" and a parameter with the name "lname", how is the compiler to determine which you actually mean? The compiler could
guess, but that would be rather unsatisfactory, I would think.

The only feasible way to support it would be to add special syntax, however, that would break consistency in the langauge, especially since the named
parameter like property setter syntax in attributes uses the name=value
method, although that is syntactically unambigious, unlike normal method
calls.

Nov 16 '05 #15

P: n/a
Cam
> > For example, calling

ticket = new FormsAuthenticationTicket
(
version = 1,
name = txtUserName.Text,
issueDate = DateTime.Now,
expiration = DateTime.Now.AddMinutes(30),
isPersistant = false,
userData = "Users",
cookiePath = FormsAuthentication.FormsCookiePath
);

says a lot more to anyone reading the code than

ticket = new FormsAuthenticationTicket
(
1,
txtUserName.Text,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
"Users",
FormsAuthentication.FormsCookiePath
);


What about

ticket = new FormsAuthenticationTicket
(
1, // version
txtUserName.Text, // name
DateTime.Now, // issueDate
DateTime.Now.AddMinutes(30), // expiration
false, // isPersistant
"Users", // userData
FormsAuthentication.FormsCookiePath // userData
);

??
Nov 16 '05 #16

P: n/a
Hi,

No always and not for sure, what if you select weirds or not explicit names
for the parameters, then you get nothing from it.

AFAIK the only language that supported this was VB IIRC, and frankly I
never saw it used extensely.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Peter Hardy" <pe**@nospam.com> wrote in message
news:Oj****************@TK2MSFTNGP12.phx.gbl...
Hi Guyz,

Thanks for the feedback. I find named parameters (hey, I don't choose the
lingo) to be a useful way to document code.

For example, calling

ticket = new FormsAuthenticationTicket
(
version = 1,
name = txtUserName.Text,
issueDate = DateTime.Now,
expiration = DateTime.Now.AddMinutes(30),
isPersistant = false,
userData = "Users",
cookiePath = FormsAuthentication.FormsCookiePath
);

says a lot more to anyone reading the code than

ticket = new FormsAuthenticationTicket
(
1,
txtUserName.Text,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
"Users",
FormsAuthentication.FormsCookiePath
);

I have this sinking feeling I'm gonna have to learn Visual Basic now ...
And
just before Christmas too :(

Cheers Pete

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:Og**************@TK2MSFTNGP09.phx.gbl...
Hi Peter,

Nop, C# does not support named parameters, only "positional"
parameters.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Peter Hardy" <pe**@nospam.com> wrote in message
news:Oh*************@TK2MSFTNGP10.phx.gbl...
> Hi Guys,
>
> Can I use named parameters in C# Methods. i.e doFoo(fname="do",
> lname="Foo");
>
> Cheers, Pete
>
>



Nov 16 '05 #17

P: n/a
Hi,
Daniel means that the expression var_a ="foo" may be interpreted of two
forms :

1- Assign "foo" to the variable var and then pass the variable var as the
parameter , this was something that VB did not have, you could not do
something like var_a = var_b = "foo" IIRC.

2- assign "foo" to the parameter var_a
the compiler has no idea which one you want to use

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Peter Hardy" <pe**@nospam.com> wrote in message
news:eL**************@tk2msftngp13.phx.gbl...
Wouldn't the complier know that lname=foo was a named parameter by virtue
of
the fact that it's included in the method call? and the compiler would now
in advance that lname is a valid parameter name from the signature of the
method being called?

I hope you don't mind me following this thread, its just I suspect there
is
a gap in my understanding and I'd like to fill it.

Cheers, Pete

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:#o**************@TK2MSFTNGP12.phx.gbl...
>
> I know I must seem like a pedant but named parameters are available in
> SmallTalk and Visual Basic but not in C# and I can't for the life of me
> think why.


The simple answer? Its not syntactially simple.

lname="Foo" is a valid assignment expression, it resolves to "Foo". If

there
is a local with the name "lname" and a parameter with the name "lname",

how
is the compiler to determine which you actually mean? The compiler could
guess, but that would be rather unsatisfactory, I would think.

The only feasible way to support it would be to add special syntax,

however,
that would break consistency in the langauge, especially since the named
parameter like property setter syntax in attributes uses the name=value
method, although that is syntactically unambigious, unlike normal method
calls.


Nov 16 '05 #18

P: n/a
yeah, thats what i'm doing now. doesn't feel so neat cos I have to read it
right to left but am happy enough. Thanks for the help guys, really do
appreciate it

Cheers, Pete

"Cam" <Ca*@discussions.microsoft.com> wrote in message
news:0E**********************************@microsof t.com...
For example, calling

ticket = new FormsAuthenticationTicket
(
version = 1,
name = txtUserName.Text,
issueDate = DateTime.Now,
expiration = DateTime.Now.AddMinutes(30),
isPersistant = false,
userData = "Users",
cookiePath = FormsAuthentication.FormsCookiePath
);

says a lot more to anyone reading the code than

ticket = new FormsAuthenticationTicket
(
1,
txtUserName.Text,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
"Users",
FormsAuthentication.FormsCookiePath
);


What about

ticket = new FormsAuthenticationTicket
(
1, // version
txtUserName.Text, // name
DateTime.Now, // issueDate
DateTime.Now.AddMinutes(30), // expiration
false, // isPersistant
"Users", // userData
FormsAuthentication.FormsCookiePath // userData
);

??

Nov 16 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.