2
votes

I have got below two public properties in my DataContract class.

I want to use this in client side. But I don't want to return this through the service. Don't I need DataMember Attribute for MyDateString?

    [DataMember]
    public DateTime MyDate { get; set; }

    public string MyDateString
    {
        get
        {
            return MyDate.ToString("dd/MM/yyyy");
        }
    }
2

2 Answers

10
votes

If you want it to be serialized correctly across your service boundary you would need to decorate it with DataMember.

If you want it for logic internal to your service, and you do not want the value to be available when this class is returned from your service, then you should NOT decorate it with a DataMember attribute.

5
votes

You need [DataMember] only on members that you want serialized. Since MyDateString will function as expected without being serialized (the backing property MyDate, on which MyDateString depends, is already serialized), you don't need it on that property.