Local variable (int) may not be initialized before access

I have the following method defined in the class:

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

What is strange to me is that I get the message “Local variable that cannot be initialized before access” from Visual Studio (I have ReSharper installed) in the IF clause. Why is this so when "res" is the type of value, and by default it should be 0? I must indicate that if I specifically set the res value to 0, then everything is OK.

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

+6
source share
5 answers

The answer is "why C # works this way" invariably "because that's what the specification says." Section 5.3.2 of the C # language specification lists variables that are not originally assigned:

  • Instance variables of initially unassigned structural variables.
  • Output parameters, including this struct instance constructor variable.
  • Local variables other than those declared in a catch or foreach clause.

Due to why this is an error, section 5.3 states

A variable must be definitely assigned at every place where its value is obtained.

If you initialize the value as int res = new int(); , you will get a default value of zero. A more general way is mentioned in other answers, which is to set it to zero. Reliance on default values ​​makes the code less readable without real benefits.

+8
source

Why is this so when "res" is the type of value, and by default it should be 0?

Although the default value is 0 in member fields, locals require an explicit value setting. The compiler will warn you that you are using it without initializing it, as this may be a mistake.

Although it can be “technically” fine with a zero initialized default value from a logical point of view, from a real point of view, the fact that you never set the value before checking it is either 1) useless ( if will never be true) or 2 ) error (you wanted to set the value, but something has changed).

In any case, a warning should help you prevent an accident.

I must indicate that if I specifically set the res value to 0, then everything is OK.

When you do this, you specifically provide a value, so the compiler no longer warns you.

+8
source

C # requires that all variables be initialized before use. For such local variables, you need to initialize them explicitly. Fields in a structure or class do not have to be initialized explicitly, because they receive a default value as part of the construction of the object.

So here you just need to do

 int res = 0; 

and all will be well.

+2
source

The problem is that an uninitialized value is not guaranteed to be 0 in C #. According to the language specification, uninitialized variables may not be used, since C # is designed to prevent you from doing obviously bad things.

The main reason why some other languages ​​(in particular, C and C ++) allow this, because this is an additional work on the compiler writer to detect and flag this condition.

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

+1
source

Initialization by default applies to members of a class / structure, which does not apply here, because res is a local variable; as a variable, not a data member, the "initialization required before use" is applied.

0
source

Source: https://habr.com/ru/post/947582/


All Articles