Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the use of square bracket "[]" in following syntax [duplicate]

Tags:

c#

This must be a very basic question but after stumbling on internet for a while, i am unable to understand the code below. I am very new to c#. what exactly is the use case of [] (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));
    }
}
like image 890
Arpit Bansal Avatar asked May 10 '13 10:05

Arpit Bansal


1 Answers

These are attributes. Basically they provide custom metadata for members. That metadata is built into the assembly, and can be fetched (by reflection) by other code which can then use the information for whatever purpose it wants.

In this particular case, they're being used to provide metadata for properties which can be specified on a command line, presumably to be consumed by this library.

If you're new to C# you might want to just ignore these for a while - although that very much depends on what kind of development you're doing. Some code relies heavily on attributes (e.g. MVC) and other code will hardly touch it.

like image 82
Jon Skeet Avatar answered Nov 15 '22 09:11

Jon Skeet