i have seen [] such brackets in c# very very rarely but when i start to learn asp.net i have seen them many times but still i couldn’t understand what they does ?
They are not part of code as using for arrays.For example [webmethods] which is just over the methods or there are some over classes. Are they part of .net or they are just tell something to CLR ? or ?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
They are used to put Attributes on classes or methods (or other stuff). That way, you can attach data to classes that should not be part of the actual class. You can read more on them here
Method 2
[] brackets are an operator in C#. The link contains more detailed information and examples of what I summarized below.
They are used for:
- Array type definition
- Access an element of an array
- They can be used as indexer parameters for any type
- They can be used to specify attributes <– This seems like what you are asking about
- They can be used for unsafe code to index an offset from a pointer
Method 3
You’re seeing .Net attributes (VB or C#), which can annotate types and members.
Method 4
They’re attributes used to annotate methods and classes.
Method 5
It is an operator which is most commonly used for indexing into some sort of collection. The common use is for indexing into an array.
They are also commonly used in C# to define attributes. These can appear above methods and classes. They are a way of defining extra behavior for that class or method.
MSDN has a good Introduction to Attributes in C#.
Method 6
They are attributes, posting from my phone so I can’t add links but just search msdn for attributes.
Method 7
As some other said, they are attributes, you should really check MSDN about them, but in short, you could say they add code that can can be executed before or after the method is executed, or not at all! They do many different things, from being conditionals to decide if the method should run or not, to do a preprocessing operation, to just adding MetaData to the code, so other libraries or the compiler can find it and do stuff with it.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0