C
cody
Why doesn't C# allow default parameters for methods?
An argument against I hear often is that the default parameters would have
to be hardbaken into the assembly, but why? The Jit can take care of this,
if the code is jitted the
"push xyz" instructions of the actual default values can be inserted.
To make things simpler and better readable I'd make all default parameters
named parameters so that you can decide for yourself why one to pass and
which not, rather than relying on massively overlaoded methods which
hopefully provide the best overload for you, for example the Image.DrawImage
method has 20 overloads.
I propose a syntax like the following:
public void Open(string path, AccessMode mode = AccessMode.Read, int
bufferSize=1024)
{
// ...
}
As you can see, path is a regular parameter which cannot be omitted, whereas
Mode and BufferSize provide default values an can be omitted when calling
the method.
Named parameters can only be declare behind all regular parameters and if
you have params parameters (variable parameterlist) is must be declared at
the end of the parameterlist. Although you can omit the named parameters if
you pass them they should appear in the same order as they were declared for
better readability.
You could call the example method like the following:
Open("text.txt", @mode=AccessMode.Write, @bufferSize=512);
Open("text.txt", @bufferSize=512);
Open("text.txt", @mode=AccessMode.Write);
Open("text.txt");
Note that an @ sign has to appear before all named parameter so that you
have no naming conflicts with other variables in the callers context.
Additionally there can not be more that one method with the same name and
the same regular parameters to avoid ambigiuty, so:
Foo(int i, string a="");
Foo(int i, double f=1.0f);
Would not be allowed because the call Foo(100) could not be resolved.
What do you think about that? Would it even be possible to do that in the
CLR or would there have be so huge changes that it can't be done in the
future?
An argument against I hear often is that the default parameters would have
to be hardbaken into the assembly, but why? The Jit can take care of this,
if the code is jitted the
"push xyz" instructions of the actual default values can be inserted.
To make things simpler and better readable I'd make all default parameters
named parameters so that you can decide for yourself why one to pass and
which not, rather than relying on massively overlaoded methods which
hopefully provide the best overload for you, for example the Image.DrawImage
method has 20 overloads.
I propose a syntax like the following:
public void Open(string path, AccessMode mode = AccessMode.Read, int
bufferSize=1024)
{
// ...
}
As you can see, path is a regular parameter which cannot be omitted, whereas
Mode and BufferSize provide default values an can be omitted when calling
the method.
Named parameters can only be declare behind all regular parameters and if
you have params parameters (variable parameterlist) is must be declared at
the end of the parameterlist. Although you can omit the named parameters if
you pass them they should appear in the same order as they were declared for
better readability.
You could call the example method like the following:
Open("text.txt", @mode=AccessMode.Write, @bufferSize=512);
Open("text.txt", @bufferSize=512);
Open("text.txt", @mode=AccessMode.Write);
Open("text.txt");
Note that an @ sign has to appear before all named parameter so that you
have no naming conflicts with other variables in the callers context.
Additionally there can not be more that one method with the same name and
the same regular parameters to avoid ambigiuty, so:
Foo(int i, string a="");
Foo(int i, double f=1.0f);
Would not be allowed because the call Foo(100) could not be resolved.
What do you think about that? Would it even be possible to do that in the
CLR or would there have be so huge changes that it can't be done in the
future?