2

I have a function structurally similar to this one:

void Foo(dynamic arg1, dynamic arg2, out dynamic result) {
    result = arg1 + arg2;
}

I'm using dynamic since this function should generically handle any type I throw at it.
But if I try to call it like so:

string Bar = null;
...
Foo("a", "b", out this.Bar);

I get an error message; I seemingly can't pass my pre-defined string Bar to the function (Cannot convert from 'out string' to 'out dynamic').
However, when I remove the 'out' keyword from the type declaration and my function call, everything works completely fine (I'm mentioning this to prove the point that everything else works perfectly fine).
I do not understand why the out modifier changes the whole situation, and dynamic suddenly can't be cast to a string, like I would be able to do it by implicitly assigning it as stated in the docs.
I'd gladly appreciate any help, there seemingly aren't a lot of people out there passing strings to a function expecting a dynamic with the out modifier...

5
  • What if the method wanted to do this? dynamic x = 1; out_var = x; This is the same reason why one can’t use pass a string to an out typed as an object. The/a solution is to use a temporary variable of the appropriate type and assign it back to the primary variable after the call (using whatever means is appropriate) as is normally required, without considering the no-longer-applicable out. Commented Feb 11, 2020 at 18:57
  • You will need to come up with some mechanism to cast the output. You may look into generics, but this question addresses something similar: stackoverflow.com/questions/1343704/…. Commented Feb 11, 2020 at 18:58
  • @user2864740 Well, if the method skrews up that's a problem I would have to deal with, but I'm making sure I'm only passing certain types with this very small-scale project. As outlined in the docs, you can convert dynamic to any type and hope it will work •ᴗ• Commented Feb 11, 2020 at 19:00
  • void Foo<T>(dynamic arg1, dynamic arg2, out T result) and the rest is the same Foo("a", "b", out this.Bar); it's because it can't be inferred... Commented Feb 11, 2020 at 19:01
  • @Çöđěxěŕ Wow, that worked like a charm! Could you add this as an answer so I can accept it? Commented Feb 11, 2020 at 19:07

1 Answer 1

3

I get an error message; I seemingly can't pass my pre-defined string Bar to the function (Cannot convert from 'out string' to 'out dynamic').

The reason for this error is because it couldn't be inferred. To get around this you could however use a generic that satisfies this.

 void Foo<T>(dynamic arg1, dynamic arg2, out T result)

All works as expected from your example...

 Foo("a", "b", out this.Bar); // "ab"
 Foo(1, 123, out int test); // test == 124
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.