Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this [Option(...)] C# attribute mean?

Tags:

c#

What does this C# attribute mean? I am mostly working with C++, and I do know about the concept of attributes in C#, but not sure about this one: It is in a class. So basically we have a property, and an attribute for it.

[Option("h", "help", HelpText = "Shows this help message")]
public bool Help { get; set; }

Thanks

like image 302
user2381422 Avatar asked Jul 19 '13 10:07

user2381422


2 Answers

This is a Command Line Option from one of the Console application libraries that help to parse command line arguments.

It might be from the Command Line Parser tool which has very similar syntax to your example.

like image 117
Davin Tryon Avatar answered Sep 24 '22 19:09

Davin Tryon


Attributes is a way to associate information with your C# code.

For example if you want to make your method a web method , you apply the webmethod attribute

[WebMethod]
void myfunction() ...

While working with web services and you want to serialize the custom objects, you can apply the serialize attribute

[Serializable]
public class MyObject {
  public int n1 = 0;
  public String str = null;
}

if your want to use the user32.dll for some windows related task , you can import the function using the dllimport attribute as follows

[DllImport("user32.dll")]
extern static void SampleMethod();

For more you can see the MSDN

like image 37
Haseeb Asif Avatar answered Sep 22 '22 19:09

Haseeb Asif