Disadvantage of making class to Serializable
Asked Answered
S

2

12

I'm using Azure Cache preview and need to make some classes Serializable.

Is there any disadvantage of making class to be Serializable - such as performance issue?

[Serializable]
public class MyClass {}

I found few related questions, but they are not about disadvantages.

  1. Are all .NET exceptions serializable?

  2. Drawbacks of marking a class as Serializable

Thank in advance

Syrupy answered 5/10, 2012 at 22:8 Comment(1)
Only disadvantage I have encountered is that it can prevent some obfuscators from obfuscating your code. If the class is marked as Serializable, the names will be left as is.Hugues
H
9

No, there is no intrinsic overhead simply in being marked as [Serializable]. The only problem I'd have is that BinaryFormatter and NetDataContractSerializer are lousy serializers, and most other serializers aren't interested in this flag (ok, I may be biased)

Hydrolysis answered 5/10, 2012 at 22:14 Comment(0)
E
5

Assuming you're talking about the cost of using the Serializable attribute rather than the actual serialization process, one drawback would be that a third party would normally assume that the class is designed to be serializable. Whilst this may be the case, if you're just marking it serializable for the sake of it (and if there's a chance a third party might interact with it), then you'd probably want to spend time ensuring that the class is suited to be serialised in an efficient manner. So it's more of a resource tradeoff than a technical one, from that point of view.

From a technical standpoint, as Marc Gravell said, just using the attribute won't really have any overhead.

Elwaine answered 5/10, 2012 at 22:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.