I'm making an initial assignment to my reference-type variable inside an awaited lambda, like so:
private class TestClass {
public int TestInt;
}
public async Task TestMethod() {
TestClass testVar;
await Task.Run(() => {
testVar = new();
});
var testInt = testVar.TestInt;
}
However, the last line gives the error "Use of unassigned local variable 'testVar'". Is there a technical reason why C#'s code analysis can't figure out that the variable is guaranteed to be assigned by that point? It's kind of annoying to have the use the !
operator wherever I first use testVar
. Is there any way to work round this if I need to first assign the variable inside an awaited lambda, and can't conveniently give it a default assignment (it's quite a complex class)?
Basically, the reason for this is that the C# flow analysis has no way of knowing that the lambda passed into Task.Run
(or any awaited method taking a lambda) will be executed for sure by the time the await
finishes, so it doesn't assume that it will be. For this reason, I need to initialize the variable with TestClass testVar = null!;
to assert that it will be non-null by the time I access it, to get rid of the warning/error.