Why can't out and params be used together? #5273
Replies: 4 comments 3 replies
-
What would this mean? How would it work? If you wrote code that called this, what would it compile down to? |
Beta Was this translation helpful? Give feedback.
-
What are you trying to achieve with void Invoke(out (string Name, object Value)[] Arglist)
{
Arglist = new (string, object)[]
{
("One", 1),
("Two", 2)
};
}
Invoke(out var results);
foreach (var (name, value) in results)
{
Console.WriteLine($"{name} = {value}");
} |
Beta Was this translation helpful? Give feedback.
-
@DavidArno - I was a little hasty, ultimately I am building a code generator and I wanted to be able to call into a common base method that is wholly general purpose. Basically we might have generated this: public IEnumerable<T> Invoke (int Arg1, double Arg2, out DateTime Arg3)
{
// code...
} and this public IEnumerable<T> Invoke (string Arg1, out double Arg2, out long Arg3)
{
// code...
} and in each case, in each of these generated method bodies, make a call to a more general method - so I was looking for a method in which every single arg was an 'out' arg and I could then support any signature. Of course you are correct, there's absolutely no need to support what I was thinking of, values can be passed back without problems and there's no need for "out" here at all. |
Beta Was this translation helpful? Give feedback.
-
Incidentally, I stumbled upon this unexpected behavior yesterday: using System;
using System.Collections.Generic;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
var objects = new List<Argument>()
{
new ("A", 123, true),
new ("B", 234, false),
new ("C", 456, true),
new ("D", 678, false)
};
var tuples = new List<(string Name, object Value, bool Out)>()
{
("A", 123, true),
("B", 234, false),
("C", 456, true),
("D", 678, false)
};
foreach (var o in objects)
{
if (o.Name == "B")
o.Value = 100;
}
foreach (var t in tuples)
{
if (t.Name == "B")
t.Value = 100;
}
}
}
public class Argument
{
public Argument(string Name, object Value, bool Out)
{
this.Name = Name;
this.Value = Value;
this.Out = Out;
}
public string Name { get; private set; }
public object Value { get; internal set; }
public bool Out { get; private set; }
}
} The assignment to |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a situation where I'm generating C# code from SQL stored procedure metadata.
Id like to be able to define a method as:
Invoke(out params (string Name, object Value)[] Arglist)
But this is not supported (removing the 'out' of course makes it fine).
Is there a deep reason? is this something that could be added to the language?
The root of this is that SQL stored procedures can themselves have 'out' parameters.
Beta Was this translation helpful? Give feedback.
All reactions