0

I have seen examples as following:

[Serializable]
public class TestFailedException : Exception { do stuff;}

Can anyone tell me the official terminology for this "[Serializable]"? Is it some kind of indexing attribute?

Thanks

Yu Zhang
  • 2,037
  • 6
  • 28
  • 42

2 Answers2

5

The terminology for these are attributes.

Attributes add some meta-data to code. They themselves do not execute, not in conventional way. They add some extra information about a method or class. When such a decorated method (or class) is called buy a different part of the code, that code can query the meta-data and perform some action accordingly.

You can use reflection to query the attributes on a method (or class). See here.

In your particular example, the [Serializable] attribute tells CLR that given an instance of this class, the value of fields within that object can be serialized, i.e. can be sent over network, or can be written to disk. In this perspective, the [Serializable] attribute does not in any way adds that functionality, or helps in the process of serialization, just that it carries a meta-data that CLR should allow serialization.

inquisitive
  • 3,549
  • 2
  • 21
  • 47
2

It's an Attribute https://msdn.microsoft.com/en-us/library/z0w1kczw.aspx

I've also heard people refer to them as Annotations but this is more of a Java term.

Lachlan Goodhew-Cook
  • 1,101
  • 17
  • 31