Meaning of text between square brackets

后端 未结 5 1569
孤街浪徒
孤街浪徒 2020-12-16 16:17

I have seen a lot of C# programs that use the [], for example [STAThread] and then the code follows. Another classic example is [DLLImport]

5条回答
  •  被撕碎了的回忆
    2020-12-16 16:50

    These are attributes.

    Attributes have many uses - [Obsolete] marks a method as obsolete and the compiler will warn you. Others like [DebuggerNonUserCode] tell nothing to the compiler and are there to let the debugger know that the code in the marked method is auto-generated.

    You can also create your own attributes and use them to mark any kind of metadata. For example, your Customer object might have an attribute [MarketingInformation("Customer is rich! Milk him good!")].

提交回复
热议问题