9

If I have the following class:

[DataContract]
public class GetColorsRS
{
    [DataMember(Name = "Colors", Order = 0, IsRequired=true)]
    public List<Color> Colors { get; set; }

    [DataMember(Name = "Errors", Order = 1, IsRequired=false)]
    public List<Error> Errors { get; set; }
}

If no errors are found in the request, I want to send back a response that does not have an Errors node, however, it passes back an Errors node that is empty. I thought this is what the IsRequired was for?

Just noticed EmitDefaultValue, is this what I am looking for?

Xaisoft
  • 45,655
  • 87
  • 279
  • 432
  • Why not just have an empty list of errors?? Basically achieves the same thing (without any messy hacks or tricks) ?? – marc_s Jun 06 '11 at 21:02
  • 2
    @marc_s, I can do that too. But setting EmitDefaultValue = false is hardly a hack or messy trick. – Xaisoft Jun 07 '11 at 01:43
  • Be careful: http://stackoverflow.com/questions/5382402/why-is-using-datamemberemitdefaultvalue-false-not-recommended – Dave Ziegler Jul 09 '13 at 16:07

1 Answers1

17

I have determined that EmitDefaultValue should be set to false if I don't want to serialize the default value of the DataMember.

 [DataMember(Name = "Errors", Order = 1, IsRequired=false,EmitDefaultValue=false)]
 public List<Error> Errors { get; set; }
Xaisoft
  • 45,655
  • 87
  • 279
  • 432