C# Attributes

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

I am trying to grasp a better underestanding of C# Attributes . I don't see
them as a very usefull capability.

Could anyone refer me to resources show me best practices and how can I
improve my code using attributes?

Thanks,
Allan
 
Attribute is a very powerful tool. you may take a look at XML Serialization
things, which use attribute quite a lot.
 
A.M said:
Hi,

I am trying to grasp a better underestanding of C# Attributes . I don't see
them as a very usefull capability.

Could anyone refer me to resources show me best practices and how can I
improve my code using attributes?

Thanks,
Allan

I can recommend You do read in .NET docs:

"Design-Time Attributes for Components"
"Attributes and Design-Time Support"

Cheers

Marcin
 
Allan,

When I first started doing C#, I didn't really see the value of attributes
right off the bat either. Once you start using them though, you may begin to
wonder how you ever managed without them.

There are so many uses for attributes, it's hard to even begin listing them,
but here are a few quick ones:

1: Generic access to class data - I have a project on SourceForge
(http://dotnetdallib.sourceforge.net) that is a data access library. Using
attributes that specify the relationship of data classes to the underlying
database, I'm able to generate dynamic SQL statements to populate data
objects from the database or to write data objects to the database.

2: Similar to 1, attributes can be used to modify the behavior of
serialization of classes using the binary or xml serialization. And there
are actually quite a few other places in .NET where attributes are used to
modify how data is handled.

3: They're used extensively in custom controls to define how the controls
interact with the property grid and other aspects of the IDE.

4: They're can be used as a form of inline documentation (such as the
Obsolete attribute)

If you check the documentation:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2004JAN.1033/cpref/html/frlrfsystemattribu
teclasshierarchy.htm

You'll see that there are a great number of classes derived from
System.Attribute. That alone should hint at how useful they are.

Pete
 
Hi Allan,

I think the community has provided much information on your issue. Do you
still have any concern on this issue?

Please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top