What is the use of square brackets "[]" in the following syntax

This should be a very simple question, but after stumbling on the Internet for a while, I cannot understand the code below. I am very new to C #. what exactly is a precedent [] (square brackets)

 class Options { [Option('f', "file", Required = true, HelpText = "Input file to be processed.")] public string InputFile { get; set; } [Option('o', "outprefix", Required = true, HelpText = "Output prefix for file.")] public string OutPreFix { get; set; } [Option('v', "verbose", DefaultValue = false, HelpText = "Prints all messages to standard output.")] public bool Verbose { get; set; } [ParserState] public IParserState LastParserState { get; set; } [HelpOption] public string GetUsage() { return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current)); } } 
+4
source share
4 answers

These are attributes . They mainly provide custom metadata for members. This metadata is built into the assembly and can be extracted (by reflection) with other code, which can then use the information for any desired purpose.

In this particular case, they are used to provide metadata for properties that can be specified on the command line, apparently consumed by this library .

If you are new to C #, you can simply ignore them for a while - although it really depends on what you are doing. Some code is heavily dependent on attributes (e.g. MVC), while other code is unlikely to touch it.

+7
source

These are attributes - they effectively determine the metadata about the member on which they are placed (be it a class, method, etc.) and can be requested using reflection. See Further Information:

+1
source

These are attributes that can be applied to types and their members. Some people will say that you β€œdecorate” a member with an attribute to provide additional information about the member. For example, data annotations can be used to provide validation of type properties.

Further reading: MSDN

You can also create your own attributes: MSDN

Hope this helps

Floor

0
source

In C #, square brackets are used before attributes indicate a method - basically metadata that can affect the behavior of a function or class. There are many different attributes for different purposes. They can do different things, but here are a few examples:

  • they can mark the class as serializable
  • make the method available through the web service
  • mark the class property as necessary (allowing the associated page to display the correct check)
  • change web service method return format between xml and json

They may also contain properties for the meta tag, so in the example above, the Option tag has several parameters that change the behavior (for example, to create a field or change the help text).

Generally speaking, you will learn about the attributes that you need as needed, so don't worry about understanding every possibility.

0
source

Source: https://habr.com/ru/post/1480093/


All Articles