What is the defined behaviour in C# for when an exception is thrown before setting the value of an out parameter, and you then try to access the parameter?
You can't do so. The variable will still not be definitely assigned, unless it's definitely assigned before the method call.
If the variable was definitely assigned before the method call, then it will still be definitely assigned - but unless the method assigned a value before the exception was thrown, the variable's value will remain untouched:
class Test
{
static void JustThrow(out int x)
{
throw new Exception();
}
static void Main()
{
int y = 10;
try
{
JustThrow(out y);
}
catch
{
// Ignore
}
Console.WriteLine(y); // Still prints 10
}
}