I keep seeing the following syntax when looking at C# code (using .NET 4.0 framework):
[XmlIgnore, Bindable(false)] public virtual FieldBase Field {get;set;}
What is the purpose of the square brackets above the property header?
[ ] Anything shown enclosed within square brackets is optional unless indicated otherwise. The square brackets themselves are not typed unless they are shown in bold. | A vertical bar that separates two or more elements indicates that any one of the elements can be typed.
In C#, you specify an attribute by placing the name of the attribute enclosed in square brackets ([]) above the declaration of the entity to which it applies. [Serializable] public class SampleClass { // Objects of this type can be serialized. }
Square brackets are used to index (access) elements in arrays and also Strings. Specifically lost[i] will evaluate to the ith item in the array named lost.
The [] operator converts the expression inside the square brackets to a string. For instance, if it is a numeric value, JavaScript converts it to a string and then uses that string as the property name, similar to the square bracket notation of objects to access their properties.
These are attributes, they can be applied to elements of your code-base and in doing so applies metadata to that thing - like descriptive declarations. These things can have multiple attributes. There are a bunch of 'built-in' attributes exposes by the .NET framework, you can however define your own.
Types that are attributes are actually defined with a fully qualified name of SuchAThingAttribute
, whereas in being applied you need only specify the name minus Attribute
which becomes SuchAThing
. And they must derive from System.Attribute
(at least to be compliant).
An attribute can have 'settings', that is, you can specify (when writing your own) which types of elements the attribute is applicable to, and whether an element can have more than one of this type of attribute or not, and so on.
The metadata of the attribute can later be got at using Reflection and GetCustomAttribute
-like methods. Links here and here show examples of doing so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With