Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 33545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:56:17+00:00 2026-05-10T13:56:17+00:00

What are attributes in .NET, what are they good for, and how do I

  • 0

What are attributes in .NET, what are they good for, and how do I create my own attributes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T13:56:17+00:00Added an answer on May 10, 2026 at 1:56 pm

    Metadata. Data about your objects/methods/properties.

    For example I might declare an Attribute called: DisplayOrder so I can easily control in what order properties should appear in the UI. I could then append it to a class and write some GUI components that extract the attributes and order the UI elements appropriately.

    public class DisplayWrapper {     private UnderlyingClass underlyingObject;      public DisplayWrapper(UnderlyingClass u)     {         underlyingObject = u;     }      [DisplayOrder(1)]     public int SomeInt     {         get         {             return underlyingObject .SomeInt;         }     }      [DisplayOrder(2)]     public DateTime SomeDate     {         get         {             return underlyingObject .SomeDate;         }     } } 

    Thereby ensuring that SomeInt is always displayed before SomeDate when working with my custom GUI components.

    However, you’ll see them most commonly used outside of the direct coding environment. For example the Windows Designer uses them extensively so it knows how to deal with custom made objects. Using the BrowsableAttribute like so:

    [Browsable(false)] public SomeCustomType DontShowThisInTheDesigner {     get{/*do something*/} } 

    Tells the designer not to list this in the available properties in the Properties window at design time for example.

    You could also use them for code-generation, pre-compile operations (such as Post-Sharp) or run-time operations such as Reflection.Emit. For example, you could write a bit of code for profiling that transparently wrapped every single call your code makes and times it. You could ‘opt-out’ of the timing via an attribute that you place on particular methods.

    public void SomeProfilingMethod(MethodInfo targetMethod, object target, params object[] args) {     bool time = true;     foreach (Attribute a in target.GetCustomAttributes())     {         if (a.GetType() is NoTimingAttribute)         {             time = false;             break;         }     }     if (time)     {         StopWatch stopWatch = new StopWatch();         stopWatch.Start();         targetMethod.Invoke(target, args);         stopWatch.Stop();         HandleTimingOutput(targetMethod, stopWatch.Duration);     }     else     {         targetMethod.Invoke(target, args);     } } 

    Declaring them is easy, just make a class that inherits from Attribute.

    public class DisplayOrderAttribute : Attribute {     private int order;      public DisplayOrderAttribute(int order)     {         this.order = order;     }      public int Order     {         get { return order; }     } } 

    And remember that when you use the attribute you can omit the suffix ‘attribute’ the compiler will add that for you.

    NOTE: Attributes don’t do anything by themselves – there needs to be some other code that uses them. Sometimes that code has been written for you but sometimes you have to write it yourself. For example, the C# compiler cares about some and certain frameworks frameworks use some (e.g. NUnit looks for [TestFixture] on a class and [Test] on a test method when loading an assembly).
    So when creating your own custom attribute be aware that it will not impact the behaviour of your code at all. You’ll need to write the other part that checks attributes (via reflection) and act on them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of custom attributes in my .NET application and I would
ASP.NET MVC 2 will support validation based on DataAnnotation attributes like this: public class
I have an ASP.NET MVC application using Authorization Attributes on Controllers and Actions. This
I want to use the .NET StringLength and Range attributes to put constraints on
I'm using the DataAnnotations attributes along with ASP.Net MVC 2 to provide model validation
I am trying to use HTML5 data- attributes in my ASP.NET MVC 1 project.
In ASP.NET MVC 2, a couple of new action filter attributes were introduced, as
How can I access metadata (dataannotations attributes) in my asp.net mvc model class from
I'm creating custom UserControl in ASP.NET and I'm using System.ComponentModel.Attributes to decorate properties with
ASP.NET MVC has good support for role-based security, but the usage of strings as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.