I have a class and one property is enum. Something like this:
//Do i need [Serializable]
public enum SexEnum
{
Male,
Female
}
[Serializable]
public class Person
{
string Name {get;set;}
SexEnum Sex {get;set;}
}
When I serialize Person with BinaryFormatter, do I need [Serializable] at the enum decleration?
It works fine without it but then why does it allow the [Serializable] attribute in the enum decleration?
.NET knows how to automatically serialize all the simple built in types so that’s why you don’t need to specify it.
I think if .NET dissallowed the serializable attribute for items that are serializable it would be more confusing. The fact that you can decide to add it or leave it out is a matter of taste.