- If a struct has no field, you can access it without initializing it.
- If a struct in the current project has a field, you will get the error "Use of unassigned local variable".
- If a struct in a different project has a field, you can access it without initializing it.
This behavior seems weirdly inconsistent. What is the reason that accessing an uninitialized struct with a field in a different project does not cause an error, but if it were declared in the same project it does? Does the language specification address this?
Here is some sample code that demonstrates the various scenarios.
Project 1
static void Main()
{
A a;
a.ToString(); // No problem
B<int> b;
b.ToString(); // Use of unassigned local variable 'b'
C<int> c;
c.ToString(); // No problem
}
public struct A
{
}
public struct B<T>
{
T[] a;
}
Project 2
public struct C<T>
{
T[] a;
}
(Using VS2017 15.4.5)