Question

I have the following method defined in a class:

public bool LogOff(string sessionId)
{
   int res;
   // Some non related code here..
   if (res == 1)
   {
      return true;
   }
   return false;
}

What's strange to me is that I'm getting a "Local variable might not be initialized before accessing" error from Visual Studio (I do have ReSharper installed) on the IF clause. Why is this the case when "res" is a value type and as such should default to 0? I should point out that if I specifically set the value of res to 0 then it's all OK.

What am I missing here? I thought these are the basics of programming but apparently I'm not familiar with the basics...

Was it helpful?

Solution

The answer to "why does C# work this way" is invariably "because that's what's in the spec." Section 5.3.2 of the C# language specification lists the variables that are not initially assigned:

  • Instance variables of initially unassigned struct variables.
  • Output parameters, including the this variable of struct instance constructors.
  • Local variables, except those declared in a catch clause or a foreach statement.

As to why this is an error, Section 5.3 states

A variable must be definitely assigned at each location where its value is obtained.

If you initialize the value as int res = new int();, you will get the default value of zero. The more common way is mentioned in other answers, which is to set it to zero explicitly. Relying on default values just makes code less readable with no real advantages.

OTHER TIPS

Why is this the case when "res" is a value type and as such should default to 0?

Even though the value defaults to 0 in member fields, locals require explicitly setting the value. The compiler will warns you that you are using it without initializing it since that's likely a bug.

While this "technically" could be fine with a zero initialized default from a logical standpoint, from a real-world standpoint, the fact that you haven't ever set the value before checking it is either 1) useless (the if would never be true) or 2) a bug (you meant to set the value, but something changed).

In either case, a warning is there to help you prevent either scenario from occurring accidentally.

I should point out that if I specifically set the value of res to 0 then it's all OK.

When you do this, you're specifically providing a value, so the compiler no longer warns you.

C# requires all variables to be initialized prior to use. For local variables like this you need to initialize them explicitly. Fields in a struct or class don't need to be initialized explicitly because they receive their default value as part of object construction.

So here, you just need to do

int res = 0;

and all will be well.

The issue is that the uninitialized value is not guaranteed to be 0 by the C#. Per the language specification, uninitialized variables may not be used because C# is designed to prevent you from doing obviously bad things.

The primary reason that some other languages (notably, C and C++) do allow this is because it is extra work on the compiler writer to detect and flag this condition.

EDIT: Here is the relevant part of the C# language spec:

Default initialization applies to class/struct members, which doesn't apply here because res is a local variable; as a variable, and not a data member, the "initialization required before use" applies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top