3

Why is this valid

public struct MyStruct
{
    public MyStruct(double value)
    {
        myField = value;
    }

    private double myField;

    public double MyProperty
    {
        get
        {
            return myField;
        }
        set
        {
            myField = value;
        }
    }
}

and this is not

public struct MyStruct
{
    public MyStruct(double value)
    {
        MyProperty = value;
    }
    public double MyProperty
    { 
        get; 
        set;
    }
}
Simon
  • 33,714
  • 21
  • 133
  • 202
  • Are you sure it has to be a struct rather than a class? – RickNZ Nov 30 '09 at 02:18
  • Possible duplicate: http://stackoverflow.com/questions/522280/c-how-can-i-set-the-value-of-auto-property-backing-fields-in-a-struct-construc – itowlson Nov 30 '09 at 02:20
  • This is also a possible duplicate poat: http://stackoverflow.com/questions/272153/why-is-it-necessary-to-call-this-on-a-struct-to-use-automatic-properties-in-c – David Hall Nov 30 '09 at 02:22
  • possible duplicate of [Automatic Properties and Structures Don't Mix?](http://stackoverflow.com/questions/420433/automatic-properties-and-structures-dont-mix) – nawfal Jun 03 '13 at 17:58

3 Answers3

3

You need this syntax:

public struct MyStruct 
{
    public MyStruct(double value) : this()
    {
        MyProperty = value;
    }

    public double MyProperty { get; set; }
}

I got that information from the following SO post.

Community
  • 1
  • 1
David Hall
  • 32,624
  • 10
  • 90
  • 127
3

Can you change your constructor to this:

public MyStruct(double value)  : this()
{
    myField = value;
}

The reason is that you cant access properties until the backing fields have been populated. By calling the default constructor the auto implemented property backing field will get populated and then you can have access to the properties. The downside is that you are now setting the property twice (once in the base constructor and once in your constructor).

If you don't need to use properties and can use fields instead then you can avoid the problems.

Leigh S
  • 1,837
  • 11
  • 17
1

you don't need the get set, if you are not going to make then do anything. They should be used for type checking. To make the secound work remove: { get; set; }

Euclid
  • 880
  • 10
  • 24
  • 1
    I still want the encapsulation provided by it being a property. removing the get;set; would change it to a field. – Simon Nov 30 '09 at 02:11
  • @Euclid - This article explains why in most cases it's a good idea to use properties instead of fields: http://csharpindepth.com/articles/chapter8/propertiesmatter.aspx – Daniel Schilling Nov 16 '11 at 19:39
  • @DanielSchilling: While classes should generally expose properties rather than fields, the same arguments don't apply to structures. Structs don't support inheritance, and all fields of every struct are always exposed to outside mutation (`struct1 = struct2` works by mutating all fields of `struct1` to match those of `struct2`, without accessing any code in `struct1`'s type). Struct methods that modify `this`, including property setters, are quirky; much of the "mutable structs are evil" talk is really "struct property setters are evil"--exposed fields avoid those problems. – supercat Nov 27 '12 at 22:58