How can I ignore a property when serializing using the DataContractSerializer?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
DataContractSerializer only serializes the members you explicitly include when a type is decorated with DataContract. That means ignoring a property is usually simple: do not mark it with DataMember. The real complexity comes from knowing when implicit serialization is active, how inheritance behaves, and how to exclude data safely without breaking contracts.
The Core Rule: Only DataMember Values Are Serialized
When a type is marked with DataContract, only members marked with DataMember participate in serialization.
InternalToken is ignored because it is not marked with DataMember.
Do Not Mix Up IgnoreDataMember
If the type is being serialized using opt-out style elsewhere, IgnoreDataMember can explicitly exclude a member. With DataContractSerializer, the more common pattern is opt-in through DataContract plus DataMember.
Explicit exclusion example:
IgnoreDataMember is useful when you want the exclusion to be obvious in code review.
Use Separate DTOs for Stronger Contracts
If a property should never leave your boundary, the best design is often not serializer configuration at all. Instead, create a DTO that only contains the fields you want to expose.
This is more explicit and safer than relying on one serializer configuration to protect sensitive fields.
Control Optional Members Carefully
Sometimes you want a property available in the model but not always present in output. EmitDefaultValue = false is not the same as ignoring a property. It only suppresses serialization when the member has its default value.
If Comment is null, it may be omitted. If it has a value, it is still serialized. That is very different from permanent exclusion.
Verify Output With Tests
Serialization behavior should be tested, especially when omitting sensitive data.
This catches accidental DataMember additions during refactors.
Inheritance and Versioning Considerations
If base classes and derived classes use data contracts, be explicit about which members belong to the serialized contract. Also remember that removing a serialized member can break consumers that expect it. Excluding a property is easy technically, but contract changes still need versioning discipline.
When compatibility matters, prefer additive changes and separate output DTOs over silent serializer rule changes.
Common Pitfalls
One common mistake is assuming all public properties are serialized even when DataContract is present. Another is using EmitDefaultValue = false and thinking that means a property is fully ignored. Developers also expose domain entities directly and hope serializer attributes will handle security. Inheritance can make contract membership less obvious if attributes are scattered. Finally, teams often skip serialized-output tests and miss accidental leakage of internal fields.
Summary
- With
DataContractSerializer, the normal way to ignore a property is to omitDataMember. - Use
IgnoreDataMemberwhen you want the exclusion to be explicit. - Do not confuse omission with
EmitDefaultValue = false. - Prefer dedicated DTOs when excluded data is sensitive or boundary-specific.
- Add serialization tests to verify that ignored fields never appear in output.
- Treat serializer configuration as part of your public contract, not just an internal detail.
Related reading
- How can I implement ISerializable in .NET 4 without violating inheritance security rules?
- How can I join int to a character-separated string in .NET?
- How can I limit Parallel.ForEach?
- How can I limit Parallel.ForEach?
- How can I load an ASP.NET GridView from multiple sources asynchronously?
- How can I make a ComboBox non-editable in .NET?
- How can I make a .NET Windows Forms application that only runs in the System Tray?
- How can I make my managed NuGet package support C/CLI projects?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.