如何在方法中添加可选参数?

时间:2021-12-28 08:35:44

Is it possible to declarate optional parameters in methods?

是否可以在方法中声明可选参数?

In delphi,for example,I can do something like:

例如,在delphi中,我可以执行以下操作:

procedure Test(p1:integer;p2:integer;p3:integer = 2;p4:integer = 4)

When I call that function I can call it with four parameters or with two parameters:

当我调用该函数时,我可以用四个参数或两个参数调用它:

Test(2,3); //p3 = 2,p4 = 4.
Test(2,3,4,5); //p3 = 4,p4 = 5;

How is that possible in C#?

这怎么可能在C#中?

7 个解决方案

#1


I'm afraid this isn't possible in C# 1 to 3. However, the good news is that because it's been a much-demanded feature (though there are certainly some who would rather not see it), Microsoft have finally decided to add it to C# 4.

我担心这在C#1到3中是不可能的。然而,好消息是,因为它是一个非常需要的功能(虽然肯定有些人宁愿不看它),微软最终决定添加它到C#4。

The C# 4 syntax goes as follows:

C#4语法如下:

public static void SayHello(string s = "Hello World!")
{
    Console.WriteLine(s);
}

Usage:

SayHello(); // Prints "Hello World!"
SayHello("Hello."); // Prints "Hello."
SayHello(s: "Hello."); // Prints "Hello."

(The last example uses a named argument, which really isn't necessary in this case, but helps when you have multiple optional parameters.)

(最后一个示例使用命名参数,在这种情况下实际上不是必需的,但是当您有多个可选参数时会有所帮助。)

You can read more about that subject on this blog post.

您可以在此博客文章中阅读有关该主题的更多信息。

#2


You'll either have to wait for C# 4.0, which supports optional parameters, or use standard overloading mechanisms:

您要么必须等待支持可选参数的C#4.0,要么使用标准的重载机制:

void Test(int p1, int p2, int p3, int p4)
{ }

void Test(int p1, int p2)
{
    Test(p1, p2, 2, 4);
}

#3


It will be possible in C# 4.0, but, untill that gets released, you can work around it by creating overloaded versions of your method:

它可能在C#4.0中,但是,直到发布,你可以通过创建方法的重载版本来解决它:

public void MyMethod( int a, int b, int c )
{
    ...
}

public void MyMethod( int a, int b)
{
   MyMethod(a, b, 4);
}

#4


You can use variable arguments

您可以使用变量参数

Use the params keyword.

使用params关键字。

void paramsExample(params int[] argsRest)
{

 if(argsRest.Length == 2) then...
 else if(argsRest.Length == 4) then...
 else error...

}

#5


using System;
using System.Runtime.InteropServices;

   public static void SayHello([Optional][DefaultParameterValue("Hello Universe!")] string s)
   {
      Console.WriteLine(s);
   }

Done! :)

#6


You can't do that yet. I think it's a feature of C# 4.0.

你不能这样做。我认为这是C#4.0的一个特性。

You can use params as a work around, but that can only be used sequentially, not the way some languages treat default parameters.

您可以使用params作为解决方法,但这只能按顺序使用,而不是某些语言处理默认参数的方式。

#7


You use overloads like this

你使用像这样的重载

Test(int p1, int p2)
Test(int p1, int p2, int p3)

You can have them call a private method like this

你可以让他们调用这样的私有方法

Test(int[] ps)

and then process the data.

然后处理数据。

Another way to handle this is to NOT use overloads/optional parameters, and instead name the methods according to what they are ment to do - it might seem like a bad tradeoff, but your code will probably get easier to read.

处理此问题的另一种方法是不使用重载/可选参数,而是根据他们要做的事情命名方法 - 这看起来似乎是一个糟糕的权衡,但您的代码可能会更容易阅读。

#1


I'm afraid this isn't possible in C# 1 to 3. However, the good news is that because it's been a much-demanded feature (though there are certainly some who would rather not see it), Microsoft have finally decided to add it to C# 4.

我担心这在C#1到3中是不可能的。然而,好消息是,因为它是一个非常需要的功能(虽然肯定有些人宁愿不看它),微软最终决定添加它到C#4。

The C# 4 syntax goes as follows:

C#4语法如下:

public static void SayHello(string s = "Hello World!")
{
    Console.WriteLine(s);
}

Usage:

SayHello(); // Prints "Hello World!"
SayHello("Hello."); // Prints "Hello."
SayHello(s: "Hello."); // Prints "Hello."

(The last example uses a named argument, which really isn't necessary in this case, but helps when you have multiple optional parameters.)

(最后一个示例使用命名参数,在这种情况下实际上不是必需的,但是当您有多个可选参数时会有所帮助。)

You can read more about that subject on this blog post.

您可以在此博客文章中阅读有关该主题的更多信息。

#2


You'll either have to wait for C# 4.0, which supports optional parameters, or use standard overloading mechanisms:

您要么必须等待支持可选参数的C#4.0,要么使用标准的重载机制:

void Test(int p1, int p2, int p3, int p4)
{ }

void Test(int p1, int p2)
{
    Test(p1, p2, 2, 4);
}

#3


It will be possible in C# 4.0, but, untill that gets released, you can work around it by creating overloaded versions of your method:

它可能在C#4.0中,但是,直到发布,你可以通过创建方法的重载版本来解决它:

public void MyMethod( int a, int b, int c )
{
    ...
}

public void MyMethod( int a, int b)
{
   MyMethod(a, b, 4);
}

#4


You can use variable arguments

您可以使用变量参数

Use the params keyword.

使用params关键字。

void paramsExample(params int[] argsRest)
{

 if(argsRest.Length == 2) then...
 else if(argsRest.Length == 4) then...
 else error...

}

#5


using System;
using System.Runtime.InteropServices;

   public static void SayHello([Optional][DefaultParameterValue("Hello Universe!")] string s)
   {
      Console.WriteLine(s);
   }

Done! :)

#6


You can't do that yet. I think it's a feature of C# 4.0.

你不能这样做。我认为这是C#4.0的一个特性。

You can use params as a work around, but that can only be used sequentially, not the way some languages treat default parameters.

您可以使用params作为解决方法,但这只能按顺序使用,而不是某些语言处理默认参数的方式。

#7


You use overloads like this

你使用像这样的重载

Test(int p1, int p2)
Test(int p1, int p2, int p3)

You can have them call a private method like this

你可以让他们调用这样的私有方法

Test(int[] ps)

and then process the data.

然后处理数据。

Another way to handle this is to NOT use overloads/optional parameters, and instead name the methods according to what they are ment to do - it might seem like a bad tradeoff, but your code will probably get easier to read.

处理此问题的另一种方法是不使用重载/可选参数,而是根据他们要做的事情命名方法 - 这看起来似乎是一个糟糕的权衡,但您的代码可能会更容易阅读。