C# 默认参数解决方法

发布于 2024-11-17 05:53:33 字数 158 浏览 7 评论 0原文

默认参数有解决方法吗?在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

九公里浅绿 2024-11-24 05:53:33

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.

莫言歌 2024-11-24 05:53:33

在 C#4 中,您也可以执行相同的操作。所以这是允许的:

int foo(int k, bool check = false){
  ...
}

在 C#4 中也可以使用命名参数,因此您可以通过多种不同的方式调用此方法:

foo(10, true);
foo(10);
foo(k: 10, check: true);
foo(check: true, k: 10);

如果您有多个可选参数,并且只想指定其中一个不是的参数,则命名参数非常有用。第一个可选的,或者提高调用方的可读性。

In C#4, you can do the same. So this is allowed:

int foo(int k, bool check = false){
  ...
}

There are also possible to use named arguments in C#4 so you can call this method in many different ways:

foo(10, true);
foo(10);
foo(k: 10, check: true);
foo(check: true, k: 10);

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.

凯凯我们等你回来 2024-11-24 05:53:33

在 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

硪扪都還晓 2024-11-24 05:53:33

如果程序集是使用 MSBuild 4.0 (VS2010) 构建的,则可以在程序集中使用可选参数,即使您的目标是 .Net 2.0 框架。

语法就像你说的:

int foo(int k, bool check = false)
{
}

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:

int foo(int k, bool check = false)
{
}
半葬歌 2024-11-24 05:53:33

详细说明 Denis Mazourick 关于使用 default 可选参数< /a> 在 C# 4.0 中以及如何将默认值编译到使用类中,请尝试这个。

使用以下代码创建一个类库并构建它:

public class ClassWithDefaultParameters {
    public string Msg { get; set; }

    public ClassWithDefaultParameters(string msg = "Hello World") {
        Msg = msg;
    }
}

public class ClassWithConstructorOverloads {
    public string Msg { get; set; }

    public ClassWithConstructorOverloads(string msg) {
        Msg = msg;
    }

    public ClassWithConstructorOverloads() : this("Hello World") {}
}

现在创建一个控制台应用程序并引用您刚刚构建的 dll(不是项目,而是实际的 dll)。将其放入您的代码中并构建控制台应用程序。

    static void Main() {
        var cwdp = new ClassWithDefaultParameters();
        var cwco = new ClassWithConstructorOverloads();

        Console.WriteLine(cwdp.Msg);
        Console.WriteLine(cwco.Msg);
   }

当您运行该应用程序时,输出将如您所料:

Hello World
Hello World

现在打开类库,并将“Hello World”更改为“Hello Europe”。重新编译库并将 dll 复制到控制台应用程序的输出文件夹。不要不要重建控制台应用程序。

当您再次运行控制台应用程序时,输出将是:

Hello World
Hello Europe

可能不是您所期望的!直到您重建控制台应用程序,这两行才会打印 Hello Europe


我不知道这一点,因此我认为我不会使用默认参数。更糟糕的是,微软并没有在 MSDN 页面上提及这一点。

To elaborate on Denis Mazourick's answer about using default optional 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:

public class ClassWithDefaultParameters {
    public string Msg { get; set; }

    public ClassWithDefaultParameters(string msg = "Hello World") {
        Msg = msg;
    }
}

public class ClassWithConstructorOverloads {
    public string Msg { get; set; }

    public ClassWithConstructorOverloads(string msg) {
        Msg = msg;
    }

    public ClassWithConstructorOverloads() : this("Hello World") {}
}

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.

    static void Main() {
        var cwdp = new ClassWithDefaultParameters();
        var cwco = new ClassWithConstructorOverloads();

        Console.WriteLine(cwdp.Msg);
        Console.WriteLine(cwco.Msg);
   }

When you run the application, the output will be as you expected:

Hello World
Hello World

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:

Hello World
Hello Europe

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.

月寒剑心 2024-11-24 05:53:33

好吧,没有更简单的方法,你可以使用 param 功能,但它也有风险。

看一下 string.Format() 的示例,您可以像这样使用它:

stringA.Format("{0} is {1}", str1, str2)

这样您可以传递任意数量的参数,但如何使用它非常棘手,并且可能很容易出错

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:

stringA.Format("{0} is {1}", str1, str2)

that way you can pass any number of params, but it is quite tricky how u consume it and could be quite error prone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文