4

Duplicate:

Why Must I Initialize All Fields in my C# struct with a Non-Default Constructor?


If in the constructor of an struct like

internal struct BimonthlyPair
{
    internal int year;
    internal int month;
    internal int count;    

    internal BimonthlyPair(int year, int month)
    {
        this.year = year;
        this.month = month;
    }
}

I don't initialize a field (count in this case) I get an error:

Field 'count' must be fully assigned before control is returned to the caller

But if I assign all fields, including count in this case, with something like

this.year = year;
this.month = month;
this.count = 0;

the error disappeared.

I think this happens because C# doesn't initialize struct fields when somebody creates a new struct object. But, why? As far as I know, it initialize variables in some other contexts, so why an struct is a different scenery?

Community
  • 1
  • 1
eKek0
  • 23,005
  • 25
  • 91
  • 119
  • 1
    @Daniel Brückner: We have a duplicate (and related) question format, please use them. – Samuel Apr 25 '09 at 13:59
  • @Samuel: What should that format be? Never heard about a specific format. – Dirk Vollmar Apr 25 '09 at 14:02
  • @divo: http://stackoverflow.com/questions/610728/how-to-handle-duplicate-questions/610795#610795 – Samuel Apr 25 '09 at 14:04
  • @Samuel: Well, I won't delete my question since I looked for an answer to it before I wrote, and I didn't find anything. As you pointed out, it is a duplicate, but mine has better redaction, I think. So, I will wait to be closed by the community. – eKek0 Apr 25 '09 at 14:11
  • I can't edit questions, so I can't modified the other to be better. – eKek0 Apr 25 '09 at 14:12
  • @Samuel: Thanks, I missed that discussion – Dirk Vollmar Apr 25 '09 at 14:16
  • @Ekeko: I never said anything about deleting your question. 2 days after a question has been closed for being a duplicate, the mods will look at it and determine if it should be deleted or kept. – Samuel Apr 25 '09 at 14:20
  • Thanks Samuel, did not know about the pattern. – Daniel Brückner Apr 25 '09 at 14:47
  • Someone just improved the title of the duplicate question - the community acts again :) – Gishu Apr 25 '09 at 14:48

3 Answers3

8

When you use the default constructor, there is an implicit guarantee that the entire value of the struct is cleared out.

When you create your own constructor, you inherit that guarantee, but no code that enforces it, so it's up to you to make sure that the entire struct has a value.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
7

I cannot answer why. But you can fix it by the use of : this().

internal struct BimonthlyPair
{
    internal int year;
    internal int month;
    internal int count;

    internal BimonthlyPair(int year, int month)
        : this()  // Add this()
    {
        this.year = year;
        this.month = month;
    }
}
Vadim
  • 21,044
  • 18
  • 65
  • 101
0

Because, unlike the classes, which are stored in the heap, The structs are value objects, and will be saved in the stack. It's the same principle of assigning a local variable before using it.

Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103