9

I am wondering about the following C#-code:

struct Structure
{
    public Structure(int a, int b)
    {
        PropertyA = a;
        PropertyB = b;
    }
    public int PropertyA { get; set; }
    public int PropertyB { get; set; }
}

It is not compiling with an error "The 'this' object cannot be used before all of its fields are assigned to". For the analogous class it is compiling without any problems.

It can be made working by refactoring to the following:

struct Structure
{
    private int _propertyA;
    private int _propertyB;

    public Structure(int a, int b)
    {
        _propertyA = a;
        _propertyB = b;
    }

    public int PropertyA
    {
        get { return _propertyA; }
        set { _propertyA = value; }
    }

    public int PropertyB
    {
        get { return _propertyB; }
        set { _propertyB = value; }
    }
}

But, I though that the whole point of introducing auto-properties to the C# was to avoid writing later code. Does that mean that auto-properties are not relevant for the structs?

Peter17
  • 3,052
  • 9
  • 47
  • 77

3 Answers3

21

In C# 6, this simply goes away; the code in the question compiles fine.


While Stefan has the answer than addresses the question, I have to advise you not to use a mutable struct - it will bite you. Mutable structs are evil.

IMO, the "correct" fix here is simply:

struct Structure
{
    public Structure(int a, int b)
    {
        propertyA = a;
        propertyB = b;
    }
    private readonly int propertyA, propertyB;
    public int PropertyA { get { return propertyA; } }
    public int PropertyB { get { return propertyB; } }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 3
    I'd just use `PropertyA { get; private set; }`. Readonly gains you very little – it's almost always trivial to avoid setting your properties more than once. And if you don't use `readonly`, you're still using automatically implemented properties, so the problem is still relevant. – Joren Feb 17 '11 at 13:06
  • 3
    @Joren - it expresses our intent ;) and avoids the annoyance of having to call `:this()` – Marc Gravell Feb 17 '11 at 15:49
  • @Joren For those that might stumble across this in the future: your suggestion does not compile if you try to set the property from within the constructor. – Eric Jul 15 '15 at 01:57
  • @Eric it does in C# 6, actually; you don't need the `:this()` any more – Marc Gravell Jul 15 '15 at 08:24
  • @Eric - It compiles fine? `struct Foo { Foo(int x) : this() { X = x; } public int X { get; private set; }` – Joren Jul 16 '15 at 07:06
  • @MarcGravell: Thankfully C# 6 also lets you use readonly auto props, finally removing the pain of having to declare and use an explicit backing field. I've always avoided `readonly` because *most* of what it did was just make my objects less clean. – Joren Jul 16 '15 at 07:08
  • @Joren It compiles because you called the base constructor which set the auto property backing field to 0. Without the this() it will not compile. – Eric Jul 16 '15 at 11:37
  • @Eric: I don't see your point. There are many things you could leave out which would make it not compile, like `struct` or `public` or the constructor body. I never suggested you could leave the `this()` call out, so I'm not sure why you're singling it out. – Joren Jul 17 '15 at 06:51
15

You need to call the default constructor first, like so:

struct Structure
{
    public Structure(int a, int b) : this()
    {
        PropertyA = a;
        PropertyB = b;
    }
    public int PropertyA { get; set; }
    public int PropertyB { get; set; }
}
Stefan Dragnev
  • 14,143
  • 6
  • 48
  • 52
8

As you've seen, when referring to PropertyA in your constructor, you're accessing the this object, which the compiler won't allow because your fields haven't been initialized yet.

To get around this, you need to find a way to initialize the fields. One way is your example: If you don't use auto-properties, then the fields are explicit and you can initialize them.

Another way is to have your constructor call another constructor that initializes the fields. Structs always implicitly have a parameterless constructor that initializes its fields to zero, so use that:

public Structure(int a, int b)
    : this()
{
    PropertyA = a;
    PropertyB = b;
}
Joren
  • 14,472
  • 3
  • 50
  • 54