Does adding [Serializable] to the class have any performance implications?
Asked Answered
A

2

29

I need to add the [Serializable] attribute to a class that is extremely performance sensitive.

Will this attribute have any performance implications on the operation of the class?

Anglice answered 10/9, 2009 at 1:0 Comment(2)
Only as long as it takes to make an instance of the attribute. Why, what have you noticed?Buyers
I am just asking ahead of time, so that I don't have to create another solution, if this had any perf issues.Anglice
N
26

Instances of attribute classes are only created when they're first accessed. If you don't do any serialization on that particular class, the SerializableAttribute() constructor will never be called, hence it won't cause any performance issues.

Here's an interesting article about attribute constructors: http://www.codingonthetrain.com/2008/10/attribute-constructors.html

Nadabus answered 10/9, 2009 at 1:5 Comment(0)
I
10

Attributes are a metadata annotations so they don't add weight to a class at runtime, unless they are interpreted by the runtime in a certain way that makes it treat the class differently.

[Serializable] is simply a marker attribute used as a convention to indicate that the class is serializable, it has no effect and the runtime does not treat the classes in any special way.

Idaline answered 10/9, 2009 at 1:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.