Is there "Default Peremeter" in C#?

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
news.microsoft.com said:
Newsbeitrag news:#[email protected]...

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....
 
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.
 
actually news.microsoft.com is the default user for posts via
msdn.microsoft.com web site.
 
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.
 
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.
 
Oh i could have fun with that :D



*g*


Michael Culley said:
Does anyone know why news server take the poster's date? Servers reject
messages that are posted in the past, but it seems possible
 
William Ryan said:
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.
 
That's great, only you aren't using the CDO interface.

Might I also add that the default user is
"(e-mail address removed)" and not "anonymouse" as you
appear to have typo'd.
 
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" <[email protected]>
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
 
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/2003/10/cs200310jh_f/cs200310jh_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!>>
 
William Ryan said:
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/2003/10/cs200310jh_f/cs200310jh_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.
 
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.
 
Back
Top