注意:这个问题是在c#还不支持可选参数的时候提出的(即在c# 4之前)。

我们正在构建一个web API,它是从c#类中编程生成的。该类有方法GetFooBar(int a, int b), API有方法GetFooBar接受查询参数,如&a=foo &b=bar。

类需要支持可选参数,而c#语言不支持这一点。最好的方法是什么?


当前回答

我必须在VB中完成。Net 2.0 Web服务。我最终将参数指定为字符串,然后将它们转换为我需要的任何参数。使用空字符串指定了可选参数。虽然不是最干净的方法,但很有效。只是要注意捕获所有可能发生的异常。

其他回答

可选参数用于方法。如果你需要一个类的可选参数,你是:

using c# 4.0: use optional arguments in the constructor of the class, a solution i prefer, since it's closer to what is done with methods, so easier to remember. here's an example: class myClass { public myClass(int myInt = 1, string myString = "wow, this is cool: i can have a default string") { // do something here if needed } } using c# versions previous to c#4.0: you should use constructor chaining (using the :this keyword), where simpler constructors lead to a "master constructor". example: class myClass { public myClass() { // this is the default constructor } public myClass(int myInt) : this(myInt, "whatever") { // do something here if needed } public myClass(string myString) : this(0, myString) { // do something here if needed } public myClass(int myInt, string myString) { // do something here if needed - this is the master constructor } }

你可以使用方法重载…

GetFooBar()
GetFooBar(int a)
GetFooBar(int a, int b)

这取决于方法的签名,我给出的例子缺少“int b”方法,因为它将具有与“int a”方法相同的签名。

你可以使用Nullable类型…

GetFooBar(int? a, int? b)

然后,您可以使用a. hasvalue检查是否设置了参数。

另一种选择是使用'params'参数。

GetFooBar(params object[] args)

如果你想要使用命名参数,就需要创建一个类型来处理它们,尽管我认为已经有一些这样的web应用程序。

在默认值不可用的情况下,添加可选参数的方法是使用.NET OptionalAttribute类- https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.optionalattribute?view=netframework-4.8

代码示例如下:

namespace OptionalParameterWithOptionalAttribute
{
    class Program
    {
        static void Main(string[] args)
        {
            //Calling the helper method Hello only with required parameters
            Hello("Vardenis", "Pavardenis");
            //Calling the helper method Hello with required and optional parameters
            Hello("Vardenis", "Pavardenis", "Palanga");
        }
        public static void Hello(string firstName, string secondName, 
            [System.Runtime.InteropServices.OptionalAttribute] string  fromCity)
        {
            string result = firstName + " " + secondName;
            if (fromCity != null)
            {
                result += " from " + fromCity;
            }
            Console.WriteLine("Hello " + result);
        }

    }
}

我有一个web服务要写,需要7个参数。每个属性都是由此web服务包装的sql语句的可选查询属性。所以我想到了两个非可选参数的变通方法…都很穷:

方法1(param1, param2, param 3, param 4, param 5, param 6, param7) 方法1(param1, param2, param3, param4, param5, param 6) 方法1(param1, param2, param3, param4, param5, param7)…开始看图片。这就是疯狂。太多的组合了。

现在有一个看起来很尴尬但应该有效的更简单的方法: method1(param1, bool useParam1, param2, bool useParam2,等等…)

这是一个方法调用,所有参数的值都是必需的,它将处理其中的每种情况。如何从界面使用它也很清楚。

这是一个黑客,但它会起作用。

在c#中,我通常会使用多种形式的方法:

void GetFooBar(int a) { int defaultBValue;  GetFooBar(a, defaultBValue); }
void GetFooBar(int a, int b)
{
 // whatever here
}

更新:上面提到的是我在c# 2.0中处理默认值的方式。我现在工作的项目使用c# 4.0,现在直接支持可选参数。下面是我在自己的代码中使用的一个例子:

public EDIDocument ApplyEDIEnvelop(EDIVanInfo sender, 
                                   EDIVanInfo receiver, 
                                   EDIDocumentInfo info,
                                   EDIDocumentType type 
                                     = new EDIDocumentType(EDIDocTypes.X12_814),
                                   bool Production = false)
{
   // My code is here
}