2

I have code that basically looks like this (it isn't actually object, but rather a custom class):

object thing
try {
  thing = new object();
    ......

} catch { stuff }
finally {
  if (thing != null) { some clean up code }

But VS is not letting me do this because it says I am referencing an unassigned variable. I am well aware it could be unassigned when this code is run, which is why the null check is there. I don't want to instantiate the object outside of the try block because it does a fair bit and could throw an exception, and I would prefer against wrapping the whole thing in another try/catch block just so I can instantiate it up there. Is there something else I can do?

Nag
  • 1,438
  • 1
  • 11
  • 39
cost
  • 4,420
  • 8
  • 48
  • 80

8 Answers8

11

"Unassigned" isn't the same as "null". Your code is simply invalid - you need to fix it.

It's very easy here - just initialize the variable to null to start with:

object thing = null;

Now it will definitely have a value (a null reference) so you're allowed to read from it in the finally block.

The point is that local variables cannot be read before the point at which the compiler can prove that a value (whether null or not) has definitely been assigned. Effectively, local variables don't have "default values".

(Mind you, I'd normally use IDisposable for clean-up code, along with a using statement.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I was unaware unassigned and null were two distinctive states, thank you. You're right about the IDisposable though, this class needs one, and it's simply on my very long list of things to do still – cost Feb 22 '12 at 12:38
  • 1
    This has been thoroughly explained here: http://stackoverflow.com/a/8933935/641530 – Ernesto Feb 22 '12 at 22:00
6

No, you can't disable that compiler error. But why don't you just initialize the variable?

object thing = null;

Unlike fields, local variables don't get initialized to null automatically. They are uninitialized, which is a special state, distinct from being null.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
4

Just do

object thing = null;

and the compiler will be placated.

For background on why the compiler complains about this, see this Eric Lippert answer, whence:

The reason this is illegal in C# is because using an unassigned local has high likelihood of being a bug.

Community
  • 1
  • 1
AakashM
  • 62,551
  • 17
  • 151
  • 186
2

how about:

    object thing = null;
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
1

What is wrong with just setting to to null initially?

object thing = null;
try {
  thing = new object();
    ......

} catch { stuff }
finally {
  if (thing != null) { some clean up code }
Thilo
  • 257,207
  • 101
  • 511
  • 656
1

Just assign null to begin with:

object thing = null; // <-- That's assignment enough
try {
  thing = new object();
  ......
} catch { stuff }
finally {
  if (thing != null) { some clean up code }
}
JimmiTh
  • 7,389
  • 3
  • 34
  • 50
1

Why not try object thing = null; as first line ?

montardon
  • 399
  • 3
  • 12
1

change your code to this and it should work ok

object thing = null;
try {
  thing = new object();
    ......

} catch { stuff }
finally {
  if (thing != null) { some clean up code }
Ben Robinson
  • 21,601
  • 5
  • 62
  • 79