简体   繁体   English

如何在方法中放置可选参数?

[英]How do I put optional parameters in methods?

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#中怎么可能?

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 . 您可以在此博客文章上了解有关该主题的更多信息

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);
}

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);
}

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...

}
using System;
using System.Runtime.InteropServices;

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

Done! 做完了! :) :)

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来解决,但是只能顺序使用,而不能使用某些语言对待默认参数的方式。

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. 解决此问题的另一种方法是不使用重载/可选参数,而是根据要执行的操作来命名方法-这似乎是一个不好的权衡,但是您的代码可能会更易于阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM