views:

34248

answers:

14

We're building a web API that's programmatically generated from a C# class (the class has method "GetFooBar(int a, int b)" and the API has a method GetFooBar taking query params like &a=foo&b=bar.

The classes needs to support optional parameters, which isn't supported in C# the language. What's the best approach?

+14  A: 

From this site:

http://www.tek-tips.com/viewthread.cfm?qid=1500861&page=1

C# does allow the use of the [Optional] attribute (from VB, though not functional in C#). So you can have a method like this:

using System.Runtime.InteropServices;
public void Foo(int a, int b, [Optional] int c)
{
  ...
}

In our API wrapper, we detect optional parameters (ParameterInfo p.IsOptional) and set a default value. The goal is to mark parameters as optional without resorting to kludges like having "optional" in the parameter name.

kurious
Won't that give a compile time error if you try to call it?
Casebash
Perfect solution I was looking for, it doesn't give any compilation error.
JPReddy
But how to use the function Foo then? Foo(1,1);foo(1,1,null) and foo(1,1,Missing.value) will all throw exceptions
Bolu
+22  A: 

In C#, I would normally use multiple forms of the method:

void GetFooBar(int a) { int defaultBValue;  GetFooBar(a, defaultBValue); }
void GetFooBar(int a, int b)
{
 // whatever here
}
stephenbayer
Called method overloading...
justin.m.chase
@just in case: you have the best username.
Jim Schubert
+9  A: 

You could use method overloading...

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

It depends on the method signatures, the example I gave is missing the "int b" only method because it would have the same signature as the "int a" method.

You could use Nullable types...

GetFooBar(int? a, int? b)

You could then check, using a.HasValue, to see if a parameter has been set.

Another option would be to use a 'params' parameter.

GetFooBar(params object[] args)

If you wanted to go with named parameters wuold would need to create a type to handle them, although I think there is already something like this for web apps (not really my area of knowledge).

Keith.

Keith Moore
+10  A: 

Another option is to use the params keyword

public void DoSomething(params object[] theObjects)
{
  foreach(object o in theObjects)
  {
    // Something with the Objects…
  }
}

EDIT:

Oh, Called like...

DoSomething(this, that, theOther);
Tim Jarvis
This answer explains the params keyword beautifilly in 10 lines, MSDN still fails to do it in 30. +1
fneep
+1  A: 

The typical way this is handled in C# as stephen mentioned is to overload the method. By creating multiple versions of the method with different parameters you effectively create optional parameters. In the forms with fewer parameters you would typically call the form of the method with all of the parameters setting your default values in the call to that method.

Yobi21
A: 

Instead of default parameters, why not just construct a dictionary class from the querystring passed .. an implementation that is almost identical to the way asp.net forms work with querystrings.

i.e. Request.QueryString["a"]

This will decouple the leaf class from the factory / boilerplate code.


You also might want to check out Web Services with ASP.NET. Web services are a web api generated automatically via attributes on C# classes.

Robert Paulson
+5  A: 

I agree with stephenbayer. But since it is a webservice, it is easier for end-user to use just one form of the webmethod, than using multiple versions of the same method. I think in this situation Nullable Types are perfect for optional parameters.

public void Foo(int a, int b, int? c)
{
  if(c.HasValue)
  {
    // do something with a,b and c
  }
  else
  {
    // do something with a and b only
  }  
}
Vivek
+1 Word of advice though. Don't make this a habit since it can get really messy.
mhenrixon
+5  A: 

Hello Optional World

If you want the runtime to supply a default parameter value, you have to use reflection to make the call. Not as nice as the other suggestions for this question, but compatible with VB.NET.

using System;
using System.Runtime.InteropServices;
using System.Reflection;

namespace ConsoleApplication1
{
    class Class1
    {
        public static void sayHelloTo(
            [Optional,
            DefaultParameterValue("world")] string whom)
        {
            Console.WriteLine("Hello " + whom);
        }

        [STAThread]
        static void Main(string[] args)
        {
            MethodInfo mi = typeof(Class1).GetMethod("sayHelloTo");
            mi.Invoke(null, new Object[] { Missing.Value });
        }
    }
}
Hugh Allen
+13  A: 

Or wait unitil C# 4.0 is released. Optional parameters are supported.

Micah
A: 

A little late to the party, but I was looking for the answer to this question and ultimately figured out yet another way to do this. Declare the data types for the optional args of your web method to be type XmlNode. If the optional arg is omitted this will be set to null, and if it's present you can get is string value by calling arg.Value, i.e.,

[WebMethod]
public string Foo(string arg1, XmlNode optarg2)
{
    string arg2 = "";
    if (optarg2 != null)
    {
        arg2 = optarg2.Value;
    }
    ... etc
}

What's also decent about this approach is the .NET generated home page for the ws still shows the argument list (though you do lose the handy text entry boxes for testing).

Ron K
Is this better than using nullable types?
Kirk Broadhurst
A: 

I have a web service to write that takes 7 parameters. Each is an optional query attribute to a sql statement wrapped by this web service. So two workarounds to non-optional params come to mind... both pretty poor:

method1(param1, param2, param 3, param 4, param 5, param 6, param7) method1(param1, param2, param3, param 4, param5, param 6) method 1(param1, param2, param3, param4, param5, param7)... start to see the picture. This way lies madness. Way too many combinations.

Now for a simpler way that looks awkward but should work: method1(param1, bool useParam1, param2, bool useParam2, etc...)

That's one method call, values for all parameters are required, and it will handle each case inside it. It's also clear how to use it from the interface.

It's a hack, but it will work.

Spanky
This is why nullable parameters exist.
Kirk Broadhurst
+2  A: 

C# 4.0 introduced optional parameters feature. For more details, please check this out. http://cherupally.blogspot.com/2009/11/c-40-new-features-named-and-optional.html

Kiran Cherupally
A: 

Use nullable parameters.

Optional parameters are unholy. Can you have two optional parameters of the same type? If you are allowed, and you include just one value, how does the runtime know which parameter you are supplying? The concept is inherently flawed.

If you're using nullable parameters, you simply pass 'null' in place of the parameter you don't want to supply.

result = MethodName(parameter1, null, parameter3)

Simple as that!

Kirk Broadhurst
I realize I'm commenting months later, but hey, you answered a couple years later in the first place! Anyway... I think optional parameters are no more unholy than overloading, they have the exact same potential pitfalls. If the variations are few and distinct based on data types, then there's no conflict and it permits for cleaner client code. If the variations/parameters are many, then chances are a class would be a more sensible way to implement it anyway. Also, if the optional parameters could be named by calling code like VB then potential conflicts disappear.
jamietre
+10  A: 

Surprised no one mentioned C# 4.0 optional parameters that work like this:

public void SomeMethod(int a, int b = 0)
{
   //some code
}
jitbit
At the time the question was asked, C# 4.0 didn't exist.
Forrest Marvez
Yep, overlooked this, sorry. But this question still ranks #1 in Google for "C# optional arguments" so anyway - this answer worth being here :)
jitbit