Compilerfout CS0269

Gebruik van niet-toegewezen parameter 'parameter'

De compiler kan niet controleren of aan de outparameter een waarde is toegewezen voordat deze werd gebruikt; de waarde ervan kan niet gedefinieerd zijn wanneer deze wordt toegewezen. Zorg ervoor dat u een waarde toewijst aan out parameters in de aangeroepen methode voordat u toegang krijgt tot de waarde. Als u de waarde van de doorgegeven variabele wilt gebruiken, gebruikt u in plaats daarvan een ref parameter. Zie Methodeparameters voor meer informatie.

Voorbeeld 1

In het volgende voorbeeld wordt CS0269 gegenereerd:

// 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);  
    }  
}  

Voorbeeld 2

Dit kan ook gebeuren als de initialisatie van een variabele plaatsvindt in een try-blok, dat de compiler niet kan verifiëren, kan worden uitgevoerd:

// 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);  
    }  
}