Why can't I assign object variables within the try block?
If I attempt to do this and clean up the variable in the finally block I get a compiler error: "use of unassigned local variable". This makes no sense because the variable is declared before the try block, and in the finally block I am first checking whether the variable is null.
Why can't the following code compile? I am checking whether dbc is null so there's no chance of it trying to do something with an unassigned variable.
eg:
DbConnection dbc;
try {
dbc = <some method call returning an open DbConnection>
// do stuff
} catch (Exception e) { // do stuff }
finally {
if (dbc != null) {
dbc.Close();
}
}