[Proposal]具有默认值的命名和可选参数 [英] [Proposal] Named and Optional Parameters with Default Values

查看:79
本文介绍了[Proposal]具有默认值的命名和可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前我有类似的想法,但现在这个需要

没有改变clr,相对容易实现并且会很棒

除了C#3.0 :)


所以我们走了......


为了让事情更简单,更易读,我会做所有默认参数

命名参数,这样你就可以自己决定要传递哪一个和

哪个不是,而不是依靠大量重叠的方法

希望为你提供最好的重载,例如Image.DrawImage

方法有20个重载。


我提出如下语法:


public void Open(字符串路径,AccessMode模式= AccessMode.Read,

int bufferSize = 1024)

{

// ...

}


如您所见,path是一个常规参数,不能省略,而

Mode和BufferSize提供默认值,可以是omitt当调用

方法时编辑。

命名参数只能在所有常规参数后面声明,如果

你有params参数(变量参数列表)必须在参数列表末尾的

处声明。虽然你可以省略命名参数,如果你传递了它们,它们应该按照声明的相同顺序出现

更好的可读性。


您可以调用如下示例方法:


Open(" text.txt",$ mode = AccessMode.Write,$ bufferSize = 512);

Open(" text.txt",$ bufferSize = 512);

Open(" text.txt",$ mode = AccessMode.Write);

打开(" text.txt");


请注意,$ sign必须出现在所有命名参数之前,以便你

有没有命名与调用者上下文中的其他变量冲突。


另外,没有更多的那个方法具有相同的名称和

相同的常规参数避免含糊不清,所以:


Foo(int i,string a ="");

Foo(int i,double f = 1.0f) ;


不允许,因为无法解析调用Foo(100)。


如何运作?

----------------------------


以下方法:


public void Open(字符串路径,AccessMode模式= AccessMode.Read,int

bufferSize = 1024)

{

// ...

}


将生成以下代码:


public struct OpenParams

{

公共AccessMode模式;

public int bufferSize;


//因为我们不能在结构中使用无参数的ctors

public static OpenParams DefaultValues

{

get {

OpenParams instance = new OpenParams();

instance.mode = AccessMode.Read;

instance.bufferSize = 1024;

返回实例;

}

}

}


public void Open( string path,ref OpenParams parms)

{

// ...

}


和电话:


Open(" text.txt",$ bufferSize = 512 );


编译为:


OpenParams parms = OpenParams.DefaultValues;

parms.bufferSize = 512 ;

开放(" text.txt",ref parms);

那么你怎么看?这只是另一个愚蠢的想法,还是值得为csharp程序员付出的努力和价值?

解决方案

< blockquote> mode = AccessMode.Write,


bufferSize = 512);

Open(" text.txt",


< blockquote> bufferSize = 512);

Open(" text.txt",


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?

解决方案

mode=AccessMode.Write,


bufferSize=512);
Open("text.txt",


bufferSize=512);
Open("text.txt",


这篇关于[Proposal]具有默认值的命名和可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆