C
cody
I got a similar idea a couple of months ago, but now this one will require
no change to the clr, is relatively easy to implement and would be a great
addition to C# 3.0
so here we go..
To make things simpler and better readable I'd make all default parameters
named parameters so that you can decide for yourself which 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 and can be omitted when calling
the method.
Named parameters can only be declared 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 ambiguity, 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.
How will it work?
----------------------------
the following method:
public void Open(string path, AccessMode mode = AccessMode.Read, int
bufferSize=1024)
{
// ...
}
would generate the following code:
public struct OpenParams
{
public AccessMode mode;
public int bufferSize;
// because we cannot have parameterless ctors in structs
public static OpenParams DefaultValues
{
get{
OpenParams instance = new OpenParams();
instance.mode = AccessMode.Read;
instance.bufferSize=1024;
return instance;
}
}
}
public void Open(string path, ref OpenParams parms)
{
// ...
}
and the call:
Open("text.txt", $bufferSize=512);
is compiled as:
OpenParams parms = OpenParams.DefaultValues;
parms.bufferSize=512;
Open("text.txt", ref parms);
So what do you think? Is it just another stupid idea or may it be worth the
effort and valueable for csharp programmers?
no change to the clr, is relatively easy to implement and would be a great
addition to C# 3.0
so here we go..
To make things simpler and better readable I'd make all default parameters
named parameters so that you can decide for yourself which 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 and can be omitted when calling
the method.
Named parameters can only be declared 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 ambiguity, 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.
How will it work?
----------------------------
the following method:
public void Open(string path, AccessMode mode = AccessMode.Read, int
bufferSize=1024)
{
// ...
}
would generate the following code:
public struct OpenParams
{
public AccessMode mode;
public int bufferSize;
// because we cannot have parameterless ctors in structs
public static OpenParams DefaultValues
{
get{
OpenParams instance = new OpenParams();
instance.mode = AccessMode.Read;
instance.bufferSize=1024;
return instance;
}
}
}
public void Open(string path, ref OpenParams parms)
{
// ...
}
and the call:
Open("text.txt", $bufferSize=512);
is compiled as:
OpenParams parms = OpenParams.DefaultValues;
parms.bufferSize=512;
Open("text.txt", ref parms);
So what do you think? Is it just another stupid idea or may it be worth the
effort and valueable for csharp programmers?