Not having a [DataMember] attribute vs having the [IgnoreDataMember] attribute

I have asked around and it seems that [IgnoreDataMember] is from the days when everything was serialised and you had to dictate what should be ignored (I believe in .Net 2). Then they changed it to the reverse and you had to explicitly state what SHOULD be serialised.

Actually that’s not quite true; IIRC it has always been both:

  • if it is marked as [DataContract], then only the members marked [DataMember] are considered
  • if it is not marked as [DataContract], then it defaults to everything, but you can subtract members using [IgnoreDataMember]

I usually just omit the [DataMember] of things that I don’t want serialized, but in many ways [IgnoreDataMember] is more explicit – mainly for the benefit of the maintainer. It says “I am intentionally not serializing this”, rather than “maybe I know that this isn’t being serialized, but maybe I just forgot to add the attribute”.

Either will work.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)