2

Do i have to put [NonSerialized] for each property of a class?

[NonSerialized]
public Cell Owner;
[NonSerialized]
public double Time
maxc900
  • 69
  • 10
  • 1
    Yes, you have to (if you want all these fields not to be serialized) – Sergey Berezovskiy Nov 29 '13 at 14:55
  • 2
    No; only the ones that you don't want to be serialized. Also, those are fields, not properties. – SLaks Nov 29 '13 at 14:58
  • 1
    `[NonSerialized]` suggests you are using `BinaryFormatter`, in which case I think the other important advice is: *be really careful there* - it can be brittle. Personally, I always advise people to use a different serializer; pretty much *any other* serializer than this one. Except `NetDataContractSerializer` - don't use that either ;p – Marc Gravell Nov 29 '13 at 15:23
  • particularly i have a list of ball class. class holds information for position, velocity and other misc. properties of ball. I want to save/resume in any moment. When resuming I need to know only position and velocity. Do you suggest any other safe and simple method other than serialization? Because i'm not a programmer and my main purpose is something else. I don't want to get drowned in programming practices and lines of codes :) – maxc900 Nov 30 '13 at 06:53

1 Answers1

2

If you look at the attribute definition (http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx):

[AttributeUsageAttribute(AttributeTargets.Field, Inherited = false)]
[ComVisibleAttribute(true)]
public sealed class NonSerializedAttribute : Attribute

AttributeTargets.Field tells you that it can be used on the fields and is not inheritable

So you have to use it on the fields that you don't want to be serializable.

Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73