Attributes Every .NET Developer Should Know About

A .NET attribute is an object, that provides a mechanism to associate information (metadata) with an element in your program. The Common Language Runtime (CLR) then uses these attributes and determines how to interact with these elements. You can also create custom attributes and use reflection to read them later.

Here’s a list of important .NET attributes that is commonly used in projects and proves very useful if you know about them beforehand. This list is just a compilation (arranged alphabetically) and clicking on each attribute will take you to a msdn link which explains its usage with an example. If you are unclear about the usage of any of the attributes listed here, drop me a comment and I will explain it.

.NET Attribute


AttributeUsageWhen you are defining your own attribute class, you can control the manner in which it is used by placing an AttributeUsageAttribute on your attribute class
CLS CompliantIndicates whether a program element is compliant with the Common Language Specification (CLS)
ConditionalIndicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined
DebuggableModifies code generation for runtime just-in-time (JIT) debugging
DebuggerDisplayDetermines how a class or field is displayed in the debugger variable windows
DebuggerStepThroughDebugger does not stop in a method marked with this attribute but does allow a breakpoint to be set in the method
DefaultValueSpecifies the default value for a property
DesignerSerializationVisibilitySpecifies the type of persistence to use when serializing a property on a component at design time
DescriptionSpecifies a description for a property or even
DisplayNameSpecifies the display name for a property, event, or public void method which takes no arguments
DllImportIndicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL) as a static entry point
EditorBrowsableSpecifies that a property or method is viewable in an editor
FlagsIndicates that an enumeration can be treated as a bit field; that is, a set of flags
InternalsVisibleToSpecifies that types that are ordinarily visible only within the current assembly are visible to a specified assembly
MTAThreadIndicates that the COM threading model for an application is multithreaded apartment (MTA)
NonSerializedIndicates that a field of a serializable class should not be serialized
ObsoleteMarks the program elements that are no longer in use
ParamArrayIndicates that a method will allow a variable number of arguments in its invocation
SerializableIndicates that a class can be serialized
WebMethodAdding this attribute to a method within an XML Web service created using ASP.NET makes the method callable from remote Web clients
WebServiceUsed to add additional information to an XML Web service, such as a string describing its functionalit
XMLIgnoreInstructs the Serialize method of the XmlSerializer not to serialize the public field or public read/write property value
XMLElementIndicates that a public field or property represents an XML element when the XmlSerializer serializes or deserializes the object that contains it
XMLRootControls XML serialization of the attribute target as an XML root element
XMLTextIndicates to the XmlSerializer that the member must be treated as XML text when the class that contains it is serialized or deserialized
ThreadStaticIndicates that the value of a static field is unique for each thread

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: