tags:

views:

311

answers:

7

Is it possible to declarate optional parameters in methods?

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

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#?

+1  A: 

You can use variable arguments

Use the params keyword.

void paramsExample(params int[] argsRest)
{

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

}
Cesar
I don't think that answers my question.
John
You can receibe variable arguments. You check if the array has 2 or 4 elements.
Cesar
Adjusted the code
Cesar
A: 

You can't do that yet. I think it's a feature of 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.

Luk
+5  A: 

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.

The C# 4 syntax goes as follows:

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.

Noldorin
Really? I'm not so sure they're highly requested. Having seen VB littered with Named/Optional parameters I am very happy that c# does NOT have them. I'm really not looking forward to them.
Chad Grant
@Deviant: I've noticed enough bloggers lamenting their absence (though also one or two stating their gladness *not* to have them, admittedly) - though I could be wrong about this. Their usage in simplifying COM method calls will be a great life, I'm sure you can't deny.
Noldorin
I second deviant - I've never understood why overloads aren't just outright better versions of the same functionality. Code to check and branch given optional args is deeply inelegant, let alone code to check permutations of optionals.
annakata
Yeah, so there's clearly some controversy over the issue. Regardless, Microsoft have made their decision as far as C# 4 goes (C# 5 could go back, of course). A particularly good usage of optional parameters, as I mentioned, is for COM calls. See the Wikipedia section on the subject for a good example: http://en.wikipedia.org/wiki/C_Sharp_(programming_language)#Optional_parameters_and_named_arguments
Noldorin
+4  A: 

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

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

void Test(int p1, int p2)
{
    Test(p1, p2, 2, 4);
}
Anton Gogolev
+4  A: 

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:

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

public void MyMethod( int a, int b)
{
   MyMethod(a, b, 4);
}
Frederik Gheysels
your first method is recursive, in an infinite loop style...
Pondidum
Indeed, stupid me. corrected :)
Frederik Gheysels
A: 

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.

Per Hornshøj-Schierbeck
+1  A: 
using System;
using System.Runtime.InteropServices;

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

Done! :)

John