Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Square bracket Syntax Above Property Declaration

Tags:

c#

.net

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?

like image 768
user559142 Avatar asked Feb 02 '12 09:02

user559142


People also ask

What does square brackets mean in syntax?

[ ] 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.

What are the square brackets above method C#?

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. }

What do square brackets mean in pseudocode?

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.

What does square brackets mean in JavaScript?

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.


1 Answers

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.

like image 188
Grant Thomas Avatar answered Oct 08 '22 03:10

Grant Thomas