5

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));
    }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Arpit Bansal
  • 75
  • 1
  • 5

4 Answers4

8

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Thanks.. This really helped. Also i would not be able to ignore them, as i have just picked an existing repo which was written in c#. and i have to understand it to rewrite it. Thanks again. – Arpit Bansal May 10 '13 at 10:55
1

These are attributes - they effectively define metadata about the member that they are placed on (be it a class, method etc.) and can be queried using reflection. See for more information:

Ant P
  • 24,820
  • 5
  • 68
  • 105
1

Those are attributes which can be applied to types and their members. Some people would say that you "decorate" a member with an attribute to provide further information about a member. For example Data Annotations can be used to provide validation against type properties.

Further reading : MSDN

You can create your own attributes too : MSDN

Hope That Helps

Paul

Paulie Waulie
  • 1,690
  • 13
  • 23
1

In C#, square brackets used before a method denote attributes - basically meta data that can affect the way a function or class behaves. There are many different kinds of attributes for many different purposes. They can do a variety of things, but here are some examples:

  • they can mark a class as serializable
  • make a method accessible via a web service
  • mark a class property as required (allowing an associate page to display the correct validation)
  • change the return format of a web service method between xml and json

They can also contain properties to do with the meta tag, so in your example above, the Option tag has several parameters being set that change the behaviour (e.g. making the field required or changing the help text).

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

Maloric
  • 5,525
  • 3
  • 31
  • 46