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

Is there "Default Peremeter" in C#?

P: n/a
It seems that when I define a fuction,I can set a default value to some of
the peremeters.When I call the fuction without some of them,the fuction will
use the default value automaticlly then continue to work.But I'v fogot how
to use,even I'v fogot whether I can use it in C# or not.
Help me.
Nov 15 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
C# has no default parameters, it uses overloading isntead.
"Clark Nu" <cl*****@hit.edu.cn> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...
It seems that when I define a fuction,I can set a default value to some of
the peremeters.When I call the fuction without some of them,the fuction will use the default value automaticlly then continue to work.But I'v fogot how to use,even I'v fogot whether I can use it in C# or not.
Help me.

Nov 15 '05 #2

P: n/a
Could you upate your clock?

José
"Clark Nu" <cl*****@hit.edu.cn> a écrit dans le message de
news:O7**************@TK2MSFTNGP10.phx.gbl...
It seems that when I define a fuction,I can set a default value to some of
the peremeters.When I call the fuction without some of them,the fuction will use the default value automaticlly then continue to work.But I'v fogot how to use,even I'v fogot whether I can use it in C# or not.
Help me.

Nov 15 '05 #3

P: n/a
"news.microsoft.com" <an********@discussions.microsoft.com> schrieb im
Newsbeitrag news:#S**************@TK2MSFTNGP11.phx.gbl...
C# has no default parameters, it uses overloading isntead.


You name isn't really news.microsoft.com, is it?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #4

P: n/a
OMG REALLY!

"codymanix" <do*********************@gmx.de> wrote in message
news:Ok*************@TK2MSFTNGP11.phx.gbl...
"news.microsoft.com" <an********@discussions.microsoft.com> schrieb im
Newsbeitrag news:#S**************@TK2MSFTNGP11.phx.gbl...
C# has no default parameters, it uses overloading isntead.


You name isn't really news.microsoft.com, is it?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Nov 15 '05 #5

P: n/a
> "news.microsoft.com" <an********@discussions.microsoft.com> schrieb im
Newsbeitrag news:#S**************@TK2MSFTNGP11.phx.gbl...
C# has no default parameters, it uses overloading isntead.


You name isn't really news.microsoft.com, is it?


Yeah, I've noticed that guy too. My suspicion is that
"news.microsoft.com" is an Agent. Everyone who has tried to fight this agent
has died. Be extremely careful. Default Params are useful, but they aren't
worth dieing for....
Nov 15 '05 #6

P: n/a
How useful compared to an overloaded method, cand you split out the common
code and just use overloads, overloading is more natural to me. I want to
..Delete() does em all or .Delete(int) does a specific one.

I find coding solutions like that more natural.

"David Sworder" <ds******@cts.com> wrote in message
news:#L*************@TK2MSFTNGP09.phx.gbl...
"news.microsoft.com" <an********@discussions.microsoft.com> schrieb im
Newsbeitrag news:#S**************@TK2MSFTNGP11.phx.gbl...
C# has no default parameters, it uses overloading isntead.
You name isn't really news.microsoft.com, is it?


Yeah, I've noticed that guy too. My suspicion is that
"news.microsoft.com" is an Agent. Everyone who has tried to fight this

agent has died. Be extremely careful. Default Params are useful, but they aren't
worth dieing for....

Nov 15 '05 #7

P: n/a
actually news.microsoft.com is the default user for posts via
msdn.microsoft.com web site.

"news.microsoft.com" <an********@discussions.microsoft.com> wrote in message
news:#l*************@tk2msftngp13.phx.gbl...
How useful compared to an overloaded method, cand you split out the common
code and just use overloads, overloading is more natural to me. I want to
.Delete() does em all or .Delete(int) does a specific one.

I find coding solutions like that more natural.

"David Sworder" <ds******@cts.com> wrote in message
news:#L*************@TK2MSFTNGP09.phx.gbl...
"news.microsoft.com" <an********@discussions.microsoft.com> schrieb im
Newsbeitrag news:#S**************@TK2MSFTNGP11.phx.gbl...
> C# has no default parameters, it uses overloading isntead.

You name isn't really news.microsoft.com, is it?


Yeah, I've noticed that guy too. My suspicion is that
"news.microsoft.com" is an Agent. Everyone who has tried to fight this

agent
has died. Be extremely careful. Default Params are useful, but they aren't worth dieing for....


Nov 15 '05 #8

P: n/a
According to C# Pro magazine, all the underlying code in VB.NET does when
you use Optional Params is write the overload for you.... I've looked at
the IL and couldn't verify it, but I'm far from an expert in IL.

I agree too, optionals are eveil.
"David Sworder" <ds******@cts.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
"news.microsoft.com" <an********@discussions.microsoft.com> schrieb im
Newsbeitrag news:#S**************@TK2MSFTNGP11.phx.gbl...
C# has no default parameters, it uses overloading isntead.
You name isn't really news.microsoft.com, is it?


Yeah, I've noticed that guy too. My suspicion is that
"news.microsoft.com" is an Agent. Everyone who has tried to fight this

agent has died. Be extremely careful. Default Params are useful, but they aren't
worth dieing for....

Nov 15 '05 #9

P: n/a
Oh, he did. To get him to the top of the list no doubt.
Nov 15 '05 #10

P: n/a
Does anyone know why news server take the poster's date? Servers reject messages that are posted in the past, but it seems possible
to post years into the future.

--
Michael Culley
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message news:u5**************@TK2MSFTNGP10.phx.gbl...
Oh, he did. To get him to the top of the list no doubt.

Nov 15 '05 #11

P: n/a
Oh i could have fun with that :D

*g*
"Michael Culley" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:ua**************@TK2MSFTNGP09.phx.gbl...
Does anyone know why news server take the poster's date? Servers reject messages that are posted in the past, but it seems possible to post years into the future.

--
Michael Culley
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message

news:u5**************@TK2MSFTNGP10.phx.gbl...
Oh, he did. To get him to the top of the list no doubt.


Nov 15 '05 #12

P: n/a

"William Ryan" <do********@nospam.comcast.net> wrote in message
news:uF**************@TK2MSFTNGP11.phx.gbl...
According to C# Pro magazine, all the underlying code in VB.NET does when
you use Optional Params is write the overload for you.... I've looked at
the IL and couldn't verify it, but I'm far from an expert in IL.
Thats not true in any manner I've ever seen. I suspect that article was
incorrect, ;). IL provides full support for declaring optional arguments,
with the OptionalAttribute attribute and the .param attribute in IL, however
it is just information, the language compiler is required to handle using
that information to use optional attributes.
I agree too, optionals are eveil.
"David Sworder" <ds******@cts.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
"news.microsoft.com" <an********@discussions.microsoft.com> schrieb im
Newsbeitrag news:#S**************@TK2MSFTNGP11.phx.gbl...
> C# has no default parameters, it uses overloading isntead.

You name isn't really news.microsoft.com, is it?


Yeah, I've noticed that guy too. My suspicion is that
"news.microsoft.com" is an Agent. Everyone who has tried to fight this

agent
has died. Be extremely careful. Default Params are useful, but they aren't worth dieing for....


Nov 15 '05 #13

P: n/a
On Sun, 26 Oct 2003 22:53:12 +0100, "news.microsoft.com"
<an********@discussions.microsoft.com> wrote:
actually news.microsoft.com is the default user for posts via
msdn.microsoft.com web site.

That's great, only you aren't using the CDO interface.

Nov 15 '05 #14

P: n/a
On Mon, 27 Oct 2003 00:07:39 GMT, "|{evin" <Yo*@dont.need> wrote:
On Sun, 26 Oct 2003 22:53:12 +0100, "news.microsoft.com"
<an********@discussions.microsoft.com> wrote:
actually news.microsoft.com is the default user for posts via
msdn.microsoft.com web site.

That's great, only you aren't using the CDO interface.


Might I also add that the default user is
"an*******@discussions.microsoft.com" and not "anonymouse" as you
appear to have typo'd.

Nov 15 '05 #15

P: n/a
"news.microsoft.com" <an********@discussions.microsoft.com> wrote in
news:O6**************@TK2MSFTNGP12.phx.gbl:
actually news.microsoft.com is the default user for posts via
msdn.microsoft.com web site.


And why do you have an Outlook Express Posting Header and a typo in
anonymous(e)???

From: "news.microsoft.com" <an********@discussions.microsoft.com>
Subject: Re: Is there "Default Peremeter" in C#?
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: h89n2fls31o1102.telia.com 217.210.97.89

--
best regards

Peter Koen
-----------------------------------
MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
http://www.kema.at
Nov 15 '05 #16

P: n/a
When I looked at the IL, I did see the option param so I wasn't sure what he
means by "Under the hood" but the inference I got was that ultimately,
that's what's happening (an Option compiles down to an overload). I reread
the article..
http://www.c-sharppro.com/features/2...200310jh_f.asp
(It's the last tip)

Here's the two parts that he mentions:

<<Method overloading lets you create multiple interface signatures for the
same method. This feature is not unique to C#. VB .NET also supports it, for
example, by letting you overload class constructors. VB .NET also uses
method overloading under the hood whenever you use the Optional keyword in a
method signature. I'll illustrate with an example. The VB .NET function
InitSqlCommand initializes and returns an ADO.NET SqlCommand object. The
function accepts an OLE DB connection string and optionally accepts a
command timeout value, which defaults to 180 seconds:>>
And :

<<The key issue here is that C# doesn't support the kinds of shortcuts VB
..NET developers are used to. The benefit of the extra work is you get a
better understanding of what the compiler is really doing. How many VB .NET
developers realize the Optional keyword is a shortcut for method
overloading? In C# you need to know!>>

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:dJYmb.37490$Tr4.73156@attbi_s03...

"William Ryan" <do********@nospam.comcast.net> wrote in message
news:uF**************@TK2MSFTNGP11.phx.gbl...
According to C# Pro magazine, all the underlying code in VB.NET does when
you use Optional Params is write the overload for you.... I've looked at the IL and couldn't verify it, but I'm far from an expert in IL.
Thats not true in any manner I've ever seen. I suspect that article was
incorrect, ;). IL provides full support for declaring optional arguments,
with the OptionalAttribute attribute and the .param attribute in IL,

however it is just information, the language compiler is required to handle using
that information to use optional attributes.
I agree too, optionals are eveil.
"David Sworder" <ds******@cts.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
> "news.microsoft.com" <an********@discussions.microsoft.com> schrieb im > Newsbeitrag news:#S**************@TK2MSFTNGP11.phx.gbl...
> > C# has no default parameters, it uses overloading isntead.
>
> You name isn't really news.microsoft.com, is it?
>

Yeah, I've noticed that guy too. My suspicion is that
"news.microsoft.com" is an Agent. Everyone who has tried to fight this

agent
has died. Be extremely careful. Default Params are useful, but they

aren't worth dieing for....



Nov 15 '05 #17

P: n/a
And i care?

"|{evin" <Yo*@dont.need> wrote in message
news:ks********************************@4ax.com...
On Sun, 26 Oct 2003 22:53:12 +0100, "news.microsoft.com"
<an********@discussions.microsoft.com> wrote:
actually news.microsoft.com is the default user for posts via
msdn.microsoft.com web site.

That's great, only you aren't using the CDO interface.

Nov 15 '05 #18

P: n/a

"William Ryan" <do********@nospam.comcast.net> wrote in message
news:uw**************@TK2MSFTNGP10.phx.gbl...
When I looked at the IL, I did see the option param so I wasn't sure what he means by "Under the hood" but the inference I got was that ultimately,
that's what's happening (an Option compiles down to an overload). I reread the article..
http://www.c-sharppro.com/features/2...200310jh_f.asp
(It's the last tip)

Here's the two parts that he mentions:

<<Method overloading lets you create multiple interface signatures for the
same method. This feature is not unique to C#. VB .NET also supports it, for example, by letting you overload class constructors. VB .NET also uses
method overloading under the hood whenever you use the Optional keyword in a method signature. I'll illustrate with an example. The VB .NET function
InitSqlCommand initializes and returns an ADO.NET SqlCommand object. The
function accepts an OLE DB connection string and optionally accepts a
command timeout value, which defaults to 180 seconds:>>
And :

<<The key issue here is that C# doesn't support the kinds of shortcuts VB
.NET developers are used to. The benefit of the extra work is you get a
better understanding of what the compiler is really doing. How many VB ..NET developers realize the Optional keyword is a shortcut for method
overloading? In C# you need to know!>>

I wonder where that came from, I don't think its accurate...There are
substantial differences between the different methods. Maybe I should write
the author and ask for clarification.
Anyway, I think we've been over this before(maybe over teh same article?) bu
heres a rundown for anyone who is interested:

Method Overloading:
Multiple methods with different parameter sets.
If used to provide default parameters, parameters are loaded in the method.
Client code does not need to recompile to effect a change in behaviour

Optional Parameters:
Single method with a single parameter set.
Can be overloaded as long as the methods differ by more than optional
parameters(in VB.NET atleast)
Default parameters are loaded by the caller. Breaks versioning in some cases
because client code must be recompiled for values to change.
here is some sample IL that shows a method with optional parameters and an
overload of that method.

optional parameters:
..method public instance void Cat([opt] string S,
[opt] string sound) cil managed
{
.param [1] = "neko"
.param [2] = "Nyao"
// Code size 16 (0x10)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: call string [mscorlib]System.String::Concat(string,
string)
IL_0008: call void [mscorlib]System.Console::WriteLine(string)
IL_000d: nop
IL_000e: nop
IL_000f: ret
} // end of method Class1::Cat

and an overload:
..method public instance void Cat(string S,
bool B) cil managed
{
// Code size 22 (0x16)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarga.s B
IL_0004: call instance string [mscorlib]System.Boolean::ToString()
IL_0009: call string [mscorlib]System.String::Concat(string,
string)
IL_000e: call void [mscorlib]System.Console::WriteLine(string)
IL_0013: nop
IL_0014: nop
IL_0015: ret
} // end of method Class1::Cat

which is the IL for the following two subs:
Public Sub Cat(Optional ByVal S As String = "neko", Optional ByVal sound
As String = "Nyao")
Console.WriteLine(S + sound)
End Sub

Public Sub Cat(ByVal S As String, ByVal B As Boolean)
Console.WriteLine(S + B.ToString())

End Sub

Optional parameters do not use overloading, they just expose a similar
interface at the programming level, atleast in all cases I can find.

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:dJYmb.37490$Tr4.73156@attbi_s03...

"William Ryan" <do********@nospam.comcast.net> wrote in message
news:uF**************@TK2MSFTNGP11.phx.gbl...
According to C# Pro magazine, all the underlying code in VB.NET does when you use Optional Params is write the overload for you.... I've looked at the IL and couldn't verify it, but I'm far from an expert in IL.

Thats not true in any manner I've ever seen. I suspect that article was
incorrect, ;). IL provides full support for declaring optional arguments, with the OptionalAttribute attribute and the .param attribute in IL,

however
it is just information, the language compiler is required to handle using that information to use optional attributes.
I agree too, optionals are eveil.

Nov 15 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.