1

I'm replacing a series of method overloads with a handful of methods with named and optional parameters.

While this is causing no problem, I am finding that there's a spanner in the works while using 'out'.

e.g.

if I was to call :

 foo(int a, out int b, int c = -1, string d = "") 

The compiler throws an error, as any time I call this method, it doesn't see it or recognise it as a relevant signature for this method.

I realise that any optional paramaters HAVE TO come after the mandatory ones -> is there any such rule for parameters with 'out', or am I missing anything obvious?

4
  • What version of Visual Studio (or framework) are you using? These features were only added in 2010. Commented Apr 26, 2011 at 9:36
  • foo(int a, out int b, int c = -1, string d = "") doesn't look a valid method definition to me, and it's certainly NOT a valid invocation. How about you show us your real code?... EDIT: Ah, it's new .NET4/VS2010 syntax. I haven't had the pleasure yet... Hell we're still upgrading from 1.1 to 2.0 ;-) Commented Apr 26, 2011 at 9:41
  • @corlettk -> Correct @Nick vs 2010/.net4 Commented Apr 26, 2011 at 10:34
  • I've just finished downloading VS2010TrainingKitMarch2011.Setup.exe I figure that Microsoft products become "stable enough to be useful" when there SP1 is released ;-) Commented Apr 26, 2011 at 10:44

1 Answer 1

3

If you mean about calling a method defined as per the example, then just (for example):

int x; foo(123, out x, d: "hi"); 

An out parameter cannot be optional (which means it must appear before the optional ones), but can be specified anywhere (as a named argument) - for example:

int x; foo(b: out x, a: 123); 

If you want b to be optional, you would need an overload:

void foo(int a, int c = -1, string d = "") { int b; foo(a, out b, c, d); } 

Now you can call:

foo(123, d: "hi"); 
Sign up to request clarification or add additional context in comments.

4 Comments

Mine looks the same format as yours? However I'm finding that If I have two methods (one an overload of the other) e.g. foo(int a, out int b, int c = -1, string d = "") and foo(int a, out int b, int c) then If I try to call the method with optional params, e.g.: int x = foo(4,5); The compiler will throw an error and point to the overload (the one with no optional params) and tell me I'm missing c....
@templater foo(4,5) works fine here; if you have a specific pair that isn't working, post them...
The specific pair combination I'm having issues with are: int DoSomethingReturnValue(Exception a, out string b, int c = -1, int d = -1, int e = -1, object[] f = null, string g = "") & int DoSomethingReturnValue(Exception a, int c, int d, int e, out string b). Anytime I'd call say, int returnedValue = DoSomethingReturnValue(ex, out desc); - the compiler will ignore the version with optional params and just inform me that DoSomethingReturnValue does not have an overload that takes 2 arguments, pointing the the overload with no optional params....
@templater - that compiles just fine here... but I have the experimental C# 5 compiler (from the Async CTP refresh). Are you sure both versions have a return value?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.