开发者

Is there a way to implement "void func(out params object[] parameters)" in C#

What I would like to do is be able to pass any number of variables into a function, manipulate those variables inside of the function, and have access to those manipulations outside of the scope of the function.

Example

void FunctionA()
{
    int x = 1;
    string s = "blah";
    int y = 4;
    FunctionB(out x, out s, out y);
    Console.WriteLine("{0}, {1}, {2}", x, s, y);
}
vo开发者_Go百科id FunctionB(out params object[] parameters)
{
    for(int ii = 0; ii < parameters.Length; ii++)
    {
        if(parameters[ii] is int) parameters[ii] = (int)parameters[ii] + 1;
    }
}

After calling FunctionA() the console should have "2, blah, 5" on it. Is there a way to do this short of returning an object[] and parsing out the variables I want?

For clarification, here is what I'm attempting to do. Pass a byte[] into a function along with a slew of variables. Attempt to read information out of the byte[] and assign it to the variables that were passed in. So if I had an int, a string, and another int written to a byte[] and I pass in something like:

int x, y;
string s;
byte[] info = ...;
FunctionB(info, x, s, y);
Console.WriteLine("{0}, {1}, {2}", x, s, y);

It would output the information from the byte[].


No, you can't.

Here you've got a single out parameter, whereas you want several separate out parameters. C# doesn't support that via params... and params is the only way of getting a varying number of parameters, unless you want to overload it up to the maximum number of parameters you want to support.

params will always copy the values of the arguments into a new array. At that point they're disassociated from the original variables.


Are you sure you need the out? Can you give us real code? This works from what I can tell about what you posted...

void Main()
{
    object [] f = new object []{ 1, "blah", 4 };
    Test( f );
    Console.WriteLine("{0}, {1}, {2}", f[0], f[1], f[2]);
    // output
    // 2, blah, 5 

}

void Test ( object[] parameters )
{
    for(int ii = 0; ii < parameters.Length; ii++)
    {
        if(parameters[ii] is int) 
            parameters[ii] = (int)parameters[ii] + 1;
    }
}


Just so you know, your "clarification" is quite different from your original question. It sounds like you're trying to write deserialization code. First, read the MSDN Serialization docs to understand more about the functionality that .NET can provide. If creating self-deserializing objects is overkill for your particular situation, you should consider using BinaryReader to read the data:

byte[] info...
int x;
string s;
int y;

using (BinaryReader reader = new BinaryReader(new MemoryStream(info)))
{
    x = reader.ReadInt32();
    s = reader.ReadString();
    y = reader.ReadInt32();
}

Note that you may have to build up your own functions on top of BinaryReader.ReadeByte() if your encoding of data doesn't match what BinaryReader expects. Also note that a function that fills "a slew of variables" from a buffer has some code-smell... surely those variables are related in some way, and you have an opportunity to define a class (with a deserializer!) that represents whatever that concept is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜