Understanding Attributes in C #


New member
Apr 26, 2014
Programming Experience
A lot of times i look at tutorials in C sharp and i understand it well , but when it comes to reading a real sample code , i get confused and i could not relate what i have read to a real code.

For example i understand that attributes are used for some kind of tagging and it is also used for deprecating methods.

Now when it comes to looks at a code line for windows phone this :
public sealed class Task
    /// <summary>
    /// You can create an integer primary key and let the SQLite control it.
    /// </summary>
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string Title { get; set; }
    public string Text { get; set; }
    public DateTime CreationDate { get; set; }
    public override string ToString()
        return Title + ":" + Text + " < " + CreationDate.ToShortDateString() + " " + CreationDate.ToShortTimeString();

where this a sample of using sqlite data base in windows phone :How to use SQLite in Windows Phone - Nokia Developer Wiki i don't get what "[PrimaryKey, AutoIncrement]" have to do with the usage of attributes as defined.

Something also like
attribute in other example... i just can't relate the definitions and too simple examples with the real code.
Last edited by a moderator:
Firstly, I have fixed the formatting of your code to make is easier to read. Please make sure that you format all code snippets in future.

As for the question, your idea of what attributes are is far too narrow. Attributes are metadata, i.e. data about data. They are not part of the data types themselves but they tell the systems using the data types something about how they can or should be used. That something can be pretty much anything you like. All that's required is an understand between the developer applying the attributes and the system reading them about what they mean.

For instance, attributes are not used for deprecating methods. There is an attribute that is intended to indicate that a member has been deprecated, but the attribute doesn't do that actual deprecating. Other systems can then read that attribute, know that the member has been deprecated and act accordingly. Two examples of such a system are the C# compiler, which will generate a warning that is displayed in the VS IDE when you build and the Sandcastle documentation system that is used to generate the MSDN documentation.

In the code you posted, those attributes indicate that that Id property corresponds to the primary key of a table and that the values in that column should be auto-incremented. That class would also have the
attribute that you mentioned to indicate that it corresponds to a table in the database. When the SQLite ADO.NET provider generates a database for you, it will know to create a table named Task with a column for each property in your Task class and that it should make the Id column an auto-incremented primary key.
Thank you for your reply, however explaining by an example is the best thing for me, an example that would tell why using attributes is better than using something else.... not just an example that would make things more complicated such most examples on the internet.

When i first learned programming years ago , i never understood classes or inheritance ...don't tell me in inheritance animal and cow every time , tell me something exciting like mario and super mario that would make me really want to use inheritance.

same thing in attributes , i hope you can tell me an example where using attributes is reasonable and there is no alternative in such case... or may be you can tell me an alternative by using more code lines but more simple basics so i can understand where the problem arose and the reason they made something like attributes.

thank you.
Have you read the relevant documentation yet? Attributes (C# and Visual Basic)
Apart from the obvious "that is how it's done" that goes for many common attributes, I think this quote from a related document is interesting:
Attributes allow you to add your own descriptive elements to C#, Visual C++, Microsoft Visual Basic 2005, or any other language that targets the runtime, without having to rewrite your compiler
definitely i have the read the documentation and i read other tutorials , but as i said it will never make sense except when i find a unique example telling me why attributes are so special...what if attributes have an alternative that has more code but i as a developer would like to use it more.
It is what it is. We use attributes because that's what you use. Microsoft saw a problem, i.e. how to provide information about code the the consumer of that code, and attributes is the solution they came up with. That's why they are used. There is no one example that is going to magically make you understand that. You either do or you don't. It's not like attributes are just one of many things that exist and we can choose any one of them. They are the one and only solution to that one general problem. There are no other options because there don't need to be.
Okay, thank you for your help, but i know when i really get to understand it that i will find it trivial ...it's just that some people like me would never be interested in something except if i knew where was it's origin and got a unique example of for it, where it was best to use it...
by the way , the reason i had a hard time understanding classes was that i learned structs first!
classes are considered next level of struts , but because of the slight but important difference that classes can have fields and methods that i didn't imagine that that something could be implemented with slight difference, that's why i prefer java language ...it's simple and much more readable and may have less features as a dynamic language that C # , but it's straight forward and much easier to learn.
Top Bottom