C#中的符号[] [英] symbol [ ] in c#
问题描述
我不理解在C#中使用[]符号,这意味着人们期待着帮助吗? (在数组声明之外)
谢谢!
谢谢!
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!
推荐答案
如果您的意思是类似于以下示例的代码,那么您正在查看属性,并且在CP上有一篇文章. C#中的属性 [
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#[^]
[STAThread]
static void Main() {
...
[DllImport("user32.dll")]
private static extern IntPtr GetLastActivePopup(IntPtr hWnd);
[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.
这个问题没有什么意义,但是方括号通常用于索引集合.首先,Google的"c#数组"将使您对它们的用途有所了解.
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.
例如,您声明:
say you declare :
byte[] bb = new byte[1024];
这意味着您要声明一个包含1024个元素的字节数组.
因此,只要您致电
bb[20]
它实际上调用数组定义中定义的索引器,该索引器将返回数组的第20个元素.
您还可以使用
创建自己的索引器
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.
这篇关于C#中的符号[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!