Assume that we have the following struct definition that uses generics:
public struct Foo<T>
{
public T First;
public T Second;
public Foo(T first)
{
this.First = first;
}
}
The compiler says
'Foo.Second' must be fully assigned before control is returned to the caller
However, if Foo is a class, then it compiles successfully.
public class Foo<T>
{
public T First;
public T Second;
public Foo(T first)
{
this.First = first;
}
}
Why? Why the compiler treats them differently? Moreover if no constructor is defined in the first Foo then it compiles. Why this behaviour?
public readonly T First; public readonly T Second;. Then it is natural to assign all instance fields in your instance constructor. If, on the other hand, you really don't want to assign to all the fields, chain the (implicit and magical) parameterless constructor, of course, like:: this()(but I guess you know that).