Reputation: 9733
Is the following code allowed? An inner struct is declared inside an outer class and new is not called for the struct. It does not crash but I got some doubt about it.
struct Inner
{
public int i;
}
private class Outer
{
public Outer() { inner.i = 10; }
private int i;
public Inner inner;
}
private static void Main(string[] arg)
{
Outer o = new Outer();
Console.WriteLine(o.inner.i);
}
Upvotes: 1
Views: 1159
Reputation: 27001
The following is cited from MSDN and shows how Structs are different from classes (see the parts I've formatted in bold, they should answer your question):
"Structs share most of the same syntax as classes, although structs are more limited than classes:
Dictionary<string, myStruct>
.new
operator.System.ValueType
, which inherits from System.Object
.Upvotes: 4
Reputation: 3279
Value types (structs are value types) automatically constructing with default value, therefore this code will be ok (if I correctly understand your question).
Upvotes: 3