Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if an out parameter was set already?

Tags:

c#

out

Is there a way to know if an out parameter was set already or not. This is the pseudocode for what I am looking for:

public virtual string blabla(long num, out bool bval)
    {
        if (!bval.HasValue)
            {
            //Do some default logic
            bval = defaultValue;
            }

        return blabla2(num, bval);
    }
like image 831
Omtara Avatar asked Apr 28 '12 18:04

Omtara


People also ask

Is it mandatory to initialise out parameter?

Generally speaking, out parameters must be initialized before the called method returns control to the caller. However, as practice shows, the compiler can make its own adjustments to this requirement.

Can out parameter be null?

No, this is not possible. If you use an out parameter then you are not permitted to read the value before assigning to the variable, for the reason the compiler has already told you. If you use ref then the parameter must be initialized before it is allowed to be passed in.

Do you need to declare an out variable before use it?

Variables passed as out arguments do not have to be initialized before being passed in a method call. However, the called method is required to assign a value before the method returns.

What is an in/out parameter?

The in, ref, and out Modifiers ref is used to state that the parameter passed may be modified by the method. in is used to state that the parameter passed cannot be modified by the method. out is used to state that the parameter passed must be modified by the method.


2 Answers

You can't - you can't read the variable until it's been definitely assigned within your method. You should think of it as being like a local variable, declared but not assigned any value at the start of the method - but which you must assign a value to before you return. (It's okay not to have assigned a value to it if an exception is thrown.)

If you want a parameter which carries information as input to the method as well as propagating information out, you should use ref instead of out.

See my article on parameter passing for more information.

like image 127
Jon Skeet Avatar answered Sep 22 '22 09:09

Jon Skeet


In addition to Jon's excellent answer, if you want the parameter to still be out, but need to see if it has been assigned a value at some place inside the method, you could use a local nullable type like follows:

public virtual string blabla(long num, out bool bval)
{
    bool? bvalLocal;

    ... //I'm assuming there is some code here that may or 
        //may not assign bvalLocal?

    // This whole if block may not be needed if the default
    // value is the default for the type (i.e. false) as
    // GetValueOrDefualt() will take care of that (see 
    // second to last line).
    if (!bvalLocal.HasValue)
    {
        //Do some default logic
        bvalLocal = defaultValue;
    }

    bval = bvalLocal.GetValueOrDefault();
    return blabla2(num, bval);
}
like image 26
Matt Avatar answered Sep 23 '22 09:09

Matt