Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of unassigned local variable: value type vs custom struct

Tags:

c#

struct

A primitive C# value type, for example int is a struct. So, why is int not initialized? There supposed to be default constructor, I think. On the other hand, a custom struct is ok.

In the following code

struct STRCT { }
class Program
{
    static void Main(string[] args)
    {
        STRCT strct;
        strct.Equals(8);
        strct.GetHashCode();
        strct.GetType();
        strct.ToString();

        int i;
        i.Equals(8);
        i.GetHashCode();
        i.GetType();
        i.ToString();
    }
}

while first 5 lines of code are ok from the C# compiler view, next 5 lines of code generates compile error:

use of unassigned local variable

Please, explain why? From my point of view both types are structs and shall have the same behavior.

like image 448
zzfima Avatar asked Dec 08 '22 19:12

zzfima


2 Answers

It's a pathological extreme of the rules of Definite Assignment. Specifically:

A struct-type variable is considered definitely assigned if each of its instance variables is considered definitely assigned.

In this case (STRCT strct), the set of instance variables is empty, and so it is true that they've all been definitely assigned.

like image 117
Damien_The_Unbeliever Avatar answered Jan 07 '23 18:01

Damien_The_Unbeliever


This is because, unlike the int, your STRCT doesn't contain any fields and therefore doesn't contain anything that could be "unassigned".

Try changing it to:

struct STRCT
{
    public int X;
}

Then you'll get the same compile error:

Error CS0165 Use of unassigned local variable 'strct' ConsoleApplication1 D:\Test\CS6\ConsoleApplication1\Program.cs 15

The C# language specification has explicit rules for "Definite Assignment" in section 5.3:

At a given location in the executable code of a function member, a variable is said to be definitely assigned if the compiler can prove, by a particular static flow analysis (§5.3.3), that the variable has been automatically initialized or has been the target of at least one assignment

And then:

A struct-type variable is considered definitely assigned if each of its instance variables is considered definitely assigned.

So from that last rule, if a struct has no instance variables (i.e. fields) then it is considered to be "definitely assigned", and therefore no compile error will be omitted.

like image 34
Matthew Watson Avatar answered Jan 07 '23 18:01

Matthew Watson