C# 默认参数解决方法
默认参数有解决方法吗?在 C++ 中,我会使用
int foo(int k, bool check = false)
一个繁琐的解决方法是重载函数。更容易的吗? (没有办法只添加变量并检查函数的调用!!)
谢谢, 太阳
Is there a workaround for default parameters? In C++ I would use
int foo(int k, bool check = false)
A tedious workaround would be to overload a function. An easier one? (There is no way just adding the variable and checking the calls of the function!!)
Thanks,
Sun
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
C#(4.0之前)不支持默认参数。即使在 c# 4.0 中,默认参数也与 C++ 中的有所不同 - 它们存储在元数据中,并且当您使用默认参数引用程序集时,它们会被编译到您的代码中。因此,如果将来更改默认值,您的代码仍将传递旧的默认值,这可能会造成不良影响。因此,请使用具有单参数和双参数的重载函数,并调用具有多个传递默认值的参数的函数。这种方法的副作用最小。
The C# (before 4.0) didn't support the default parameters. Even in c# 4.0 the default parameters are a bit different than in C++ - they're stored in metadata and, when you reference the assembly with default parameters, they're compiled into your code. So, if the default value was changed in the future, your code will still pass the OLD default value, which may cause the bad effect. So, use the overloaded functions with a single parameter and double parameters and call the one with more parameters passing the default value. Such approach will have a least side effect.
在 C#4 中,您也可以执行相同的操作。所以这是允许的:
在 C#4 中也可以使用命名参数,因此您可以通过多种不同的方式调用此方法:
如果您有多个可选参数,并且只想指定其中一个不是的参数,则命名参数非常有用。第一个可选的,或者提高调用方的可读性。
In C#4, you can do the same. So this is allowed:
There are also possible to use named arguments in C#4 so you can call this method in many different ways:
named arguments are useful if you have several optional parameters, and only want to specify one of them that is not the first optional one, or to improve readability on the calling side.
在 C# 4.0 中,现在支持默认参数和命名参数。
http://msdn.microsoft.com/en-us/library/dd264739.aspx
In C# 4.0 default and named parameters is supported now.
http://msdn.microsoft.com/en-us/library/dd264739.aspx
如果程序集是使用 MSBuild 4.0 (VS2010) 构建的,则可以在程序集中使用可选参数,即使您的目标是 .Net 2.0 框架。
语法就像你说的:
You can use optional parameters in your assemblies if they are build with MSBuild 4.0 (VS2010) even if you are targeting the .Net 2.0 framework.
The syntax is just like you said:
详细说明 Denis Mazourick 关于使用
default可选参数< /a> 在 C# 4.0 中以及如何将默认值编译到使用类中,请尝试这个。使用以下代码创建一个类库并构建它:
现在创建一个控制台应用程序并引用您刚刚构建的 dll(不是项目,而是实际的 dll)。将其放入您的代码中并构建控制台应用程序。
当您运行该应用程序时,输出将如您所料:
现在打开类库,并将“Hello World”更改为“Hello Europe”。重新编译库并将 dll 复制到控制台应用程序的输出文件夹。不要不要重建控制台应用程序。
当您再次运行控制台应用程序时,输出将是:
可能不是您所期望的!直到您重建控制台应用程序,这两行才会打印
Hello Europe
。我不知道这一点,因此我认为我不会使用默认参数。更糟糕的是,微软并没有在 MSDN 页面上提及这一点。
To elaborate on Denis Mazourick's answer about using
defaultoptional parameters in C# 4.0 and how the default values get compiled into the consuming class, try this.Create a class library with the following code and build it:
Now create a console application and reference the dll you just built (not the project, but the actual dll). Place this in your code and build the console application.
When you run the application, the output will be as you expected:
Now open up the class library, and change both "Hello World" in "Hello Europe". Recompile the library and copy the dll to the output folder of the console application. Do not rebuild the console application.
When you run the console application again, the output will be:
Probably not what you expected! It's not until you rebuild the console application that both lines will print
Hello Europe
.I didn't know this and I think I won't use the default parameters because of this. What's worse is that Microsoft doesn't mention this on the MSDN page.
好吧,没有更简单的方法,你可以使用
param
功能,但它也有风险。看一下
string.Format()
的示例,您可以像这样使用它:这样您可以传递任意数量的参数,但如何使用它非常棘手,并且可能很容易出错
well, there is no easier way, you could use
param
feature, but it is risky as well.have a look at example for
string.Format()
where you can use it like:that way you can pass any number of params, but it is quite tricky how u consume it and could be quite error prone