+3  A: 

These two attributes are used for opposite sides of the serialization equation.

When you use [NonSerialized], you're saying "this field should not be serialized at all" - so it's more of a "save time" attribute. Basically, you're saying that field does not matter for the serialized state of the object.

When you use [OptionalField], on the other hand, you're still going to serialize the field. However, if the field is missing at read time (when the stream is deserialized into an object), then no exception will be raised. This attribute is really intended to allow you to add a new field to an existing serializable type, without breaking compatibility. Older versions of the object (which are missing that field) will be deserialized normally.

Reed Copsey
thanks, thats what I had in mind, I just tght there might be something else that I might be missing ....... : )
IbrarMumtaz
@IbrarMumtaz: Nope - the difference is in your intent. [OptionalField] still suggests that the field has an effect on state, but [NotSerialized] really means that the field is something that just shouldn't be there, no matter what...
Reed Copsey
`[NonSerialized]` may even mean "this field refers to an object that can't be serialized, so we'd get an exception if we tried to serialize it".
Daniel Earwicker
@ Thanks Reed .... what about serialization events and IdeserilizationCallback interfaces??? How do these two differ now?
IbrarMumtaz
@IbrarMumtax: That really should be a completely separate question ;)
Reed Copsey
A: 

Just playing off the English language, not required and optional mean the same thing in this case.

For your first question, you pretty much nailed it on the head. [OptionalField] basically allows older serializations to be compatible with newer definitions. [NonSerialized] means that you won't find it in the serialized data.

Given the differences, I can't imagine why you'd put both on a single field but I would guess the compiler would complain.

Austin Salonen
The compiler wouldn't give two hoots. The *runtime* might not like it (I haven't tried).
Marc Gravell
You would never use both, just one or other, question is which?
IbrarMumtaz