Question

Why is the code below

private static List<WorkflowVariableDataSet> MergeDatasetsListBranch(out List<WorkflowVariableDataSet> datasetsList)
{
    if(datasetsList == null)
        datasetsList=new List<WorkflowVariableDataSet>();
    datasetsList=new List<WorkflowVariableDataSet>();
    return datasetsList;
}

generating an error at the first if statement:

Out parameter 'datasetsList' might not be initialized before accessing.

I know it should be uninitialized at this point, but the word might suggest that the error lies in possible uninitialized object accessing (when it's not even accessed, it's the reference, that is checked). Ofc that doesn't happen with ref keyword, but I'm curious how is the reference checking violating out-parameters policy.

EDIT I've edited the question and the example: the out object will be initialized inside the method anyway. The question is: WHY uninitialized object cannot be null compared? How is that different from:

object o;
if(o==null)
    ...
Was it helpful?

Solution

Compiler Error CS0269

Use of unassigned out parameter 'parameter' The compiler could not verify that the out parameter was assigned a value before it was used; its value may be undefined when assigned. Be sure to assign a value to out parameters in the called method before accessing the value. If you need to use the value of the variable passed in, use a ref parameter instead.

So treat an out-parameter as unassigned. You are the one who is responsible.

So just remove the if:

datasetsList = new List<WorkflowVariableDataSet>();

If you want to process a list that is passed to this method use ref intead (as suggested above):

OTHER TIPS

Because whether you've buggy code that never initialises the parameter, or buggy code that sometimes doesn't initialise it, it's still the same bug.

There's no point having a separate error message for the same bug depending on whether it hits in all or just one code paths; if there is a single code-path where the parameter is used before initialising, then it's has that error, and if there isn't a single code-path, then it doesn't.

So if we consider:

private static List<WorkflowVariableDataSet> MergeDatasetsListBranch(out List<WorkflowVariableDataSet> datasetsList)
{
    if(_someBooleanField)
       datasetsList = null;
    if(datasetsList == null)
        datasetsList=new List<WorkflowVariableDataSet>();
    return datasetsList;
}

Here the use of an uninitialised out parameter might or might not happen, but that suffices to mean it has the same error.

As far as the error goes, there really isn't any significant difference between these two cases.

And therefore the error message uses might, even in cases where it will always apply.

Out arguments need not be initialized prior to being passed, the calling method is required to assign a value before the method returns.

Modified your code

 private static List<WorkflowVariableDataSet> MergeDatasetsListBranch(out List<WorkflowVariableDataSet> datasetsList)
            {
                return datasetsList = new List<WorkflowVariableDataSet>();
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top