Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out parameter might not be initialized before accessing

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)
    ...
like image 890
Tarec Avatar asked Feb 05 '14 12:02

Tarec


People also ask

Is it mandatory to initialise out parameter?

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.

Can we initialize out parameter in C#?

In C# 7.0, the out parameter can pass without its declaration and initialization which is termed as the In-line declaration of Out parameter or Implicit Type Out Parameter. Its scope is limited to the method body i.e. local scope. The out parameter is allowed to use var type in the method parameter list.

Should we initialize an out parameter before a method returns?

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.

What is out parameter in C#?

The out parameter in C# is used to pass arguments to methods by reference. It differs from the ref keyword in that it does not require parameter variables to be initialized before they are passed to a method. The out keyword must be explicitly declared in the method's definition​ as well as in the calling method.


2 Answers

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):

like image 140
Tim Schmelter Avatar answered Oct 13 '22 01:10

Tim Schmelter


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.

like image 33
Jon Hanna Avatar answered Oct 12 '22 23:10

Jon Hanna