When using out parameters in a function, is it good practice to initialize them in the function?

Posted by adambox on Stack Overflow See other posts from Stack Overflow or by adambox
Published on 2010-03-09T20:18:10Z Indexed on 2010/03/28 21:43 UTC
Read the original article Hit count: 152

Filed under:
|
|
|

I have a function that uses out parameters to return multiple values to the caller. I would like to initialize them in the function, but I wasn't sure if that's a bad idea since you don't know when you call the function that it's going to change the values right away. The caller might assume that after the function returns, if whatever it was doing didn't work, the values would be whatever they were initialized to in the caller.

Is it ok / good for me to initialize in the function?

Example:

public static void SomeFunction(int ixID, out string sSomething)
{
    sSomething = "";
    sSomething = something(ixID);

    if (sSomething = "")
    {
        somethingelse();
        sSomething = "bar"
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET