I was told by someone that a blank constructor was required for serializable objects that included getters and setters, such as below:
[DataContract]
public class Item
{
[DataMember]
public string description { get; set; }
public Item() {}
public Item(string description)
{
this.description = description;
}
}
And the reason this was told me was that this allowed for construction of objects using the setter. However, I have found that the Item when defined like this:
[DataContract]
public class Item
{
[DataMember]
public string description { get; set; }
public Item(string description)
{
this.description = description;
}
}
Can be constructed without calling the constructor, when made available as a proxy class via a WCF service reference:
Item item = new Item {description = "Some description"};
Questions:
- What exactly is that block of code I'm writing after declaring
new Item
- Is a blank constructor required for [DataContract] classes? If so, what does this blank constructor do?
I have found that I can't create an object without the constructor if the class is NOT a proxy class.