views:

354

answers:

7

Hello

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 ?

+3  A: 

[] 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
Brian R. Bondy
That link goes to SO podcasts (?)
James Kolpack
That link is less than helpful.
DOK
Thanks fixed the link, ctrl+c didn't work and didn't notice I pasted the wrong link.
Brian R. Bondy
I don't think you read the question to the end...
Thomas Levesque
@Thomas: I did, I just provided more info. I explicitly specified though now which one he is seeing.
Brian R. Bondy
+16  A: 

You're seeing .Net attributes, which can annotate types and members.

SLaks
+4  A: 

They're attributes used to annotate methods and classes.

sepp2k
A: 

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

Brendan Enrick
again, he says he's not talking of arrys, but .Net attributes. EDIT: took -1 since now the answer has a relevant part.
Francisco Noriega
+7  A: 

They are used to put Attributes on classes or methods (or other stuff). Thay way, you can attach data to classes that should not be part of the actual class. You can read more on them here.

Jouke van der Maas
Thanks for all answers.Honestly i have used them one time for a debugging and it helped me better debugging under visual studio but i hadn't understand it enough .I also heard a little that it is attribute but i didn't know what attribute does.I hope that i will not encounter new structures which i have to know :) .Now i am reading links.
Freshblood
+1  A: 

They are attributes, posting from my phone so I can't add links but just search msdn for attributes.

Ben Robinson
+1  A: 

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.

Francisco Noriega
I am someone who can not get help enough from MSDN really :).I know that it is caused by my poor english.I expected very very simple samples in c#.
Freshblood