Click here to Skip to main content
15,921,646 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
I do not understand the symbols [] are used like in c # and what it would mean people are looking forward to for help? (Outside the array declaration)
thanks!
thanks!
Posted
Updated 24-Sep-10 9:50am
v2
Comments
AspDotNetDev 24-Sep-10 15:54pm    
Do you have an example of some code you don't understand that makes use of square brackets?

If you mean code like the examples below then you are looking at attributes and there is an article here on CP. Attributes in C#[^]
C#
[STAThread]
static void Main() {
...

C#
[DllImport("user32.dll")]
private static extern IntPtr GetLastActivePopup(IntPtr hWnd);

C#
[System.ComponentModel.Description("Default directory for data files")]
[System.ComponentModel.Editor(typeof(System.Windows.Forms.Design.FolderNameEditor),
  typeof(System.Drawing.Design.UITypeEditor))]
public String DataDirectory { get; set; }


Alan.
 
Share this answer
 
Comments
AspDotNetDev 24-Sep-10 21:52pm    
Nice, I had forgotten about that use of square brackets.
That question doesn't make a whole lot of sense, but the square brackets are typically used to index collections. As a starting point, Google "c# arrays" and that will give you a good idea of what they are used for.
 
Share this answer
 
Comments
AspDotNetDev 24-Sep-10 15:53pm    
Also, they can be used as an indexer on a class.
say you declare :

byte[] bb = new byte[1024];


That means you are declaring an array of bytes with 1024 elements.

So whenever you call

bb[20] it actually calls indexer defined within the array definition which sends back the 20th element of the array.

You can also create your own Indexer using

public T this[]
{
  get { .... }
}


This will ensure you to call in similar way for you own object as you do for arrays.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900