Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Circular reference in struct of instance type not allowed but circular reference of static type allowed?

Tags:

c#

why can we have a static circular reference in struct but not a instance type circular reference ?

struct C
{
    //following line is not allowed. Compile time error.
    // it's a non static circular reference.
    public C c1;
    //But this line compiles fine.
    //static circular reference.
    public static C c2;
}
like image 465
naveen Avatar asked Jan 08 '14 09:01

naveen


1 Answers

The non-static reference fails because you're trying to make the structure a part of itself, which results in the circular reference.

The static declaration works because c2 is not a part of the structure itself; whenever you declare e.g. C foo, c2 does not affect the size of foo.

like image 184
Drew McGowen Avatar answered Oct 30 '22 12:10

Drew McGowen