Share via


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. For more information, see Passing Parameters (C# Programming Guide).

Example

The following sample generates CS0269:

// CS0269.cs
class C
{
    public static void F(out int i)
    // One way to resolve the error is to use a ref parameter instead
    // of an out parameter.
    // public static void F(ref int i)
    {
        // The following line causes a compiler error because no value
        // has been assigned to i.
        int k = i;  // CS0269
        i = 1;
        // The error does not occur if the order of the two previous 
        // lines is reversed.
    }

    public static void Main()
    {
        int myInt = 1;
        F(out myInt);
        // If the declaration of method F is changed to require a ref
        // parameter, ref must be specified in the call as well.
        //F(ref myInt);
    }
}

This could also occur if the initialization of a variable occurs in a try block, which the compiler is unable to verify will execute successfully:

// CS0269b.cs
class C
{
    public static void F(out int i)
    {
        try
        {
            // Assignment occurs, but compiler can't verify it
            i = 1;
        }
        catch
        {
        }

        int k = i;  // CS0269
        i = 1;
    }

    public static void Main()
    {
        int myInt;
        F(out myInt);
    }
}

Change History

Date

History

Reason

July 2009

Expanded the introduction and the first example.

Customer feedback.