5 Very Useful C# Attributes

Trusted By

Welcome Back! I hope you enjoy the content on this site. If you have not done so already, you may want to subscribe to my RSS feed or become a fan of this blog on Facebook. Thanks for visiting!

DebuggerDisplayAttribute

Namespace: System.Diagnostics

Usage: The DebuggerDisplayAttribute can be a sweet shortcut to avoid expanding the object to get to the value of a given property when debugging. All you have to do is mouse over the object and any property defined in the attribute will show up with it’s value.

image

Sample:

    [DebuggerDisplay("ProductName = {ProductName},ProductSKU= {ProductSKU}")]
    public class Product
   
{
        public string ProductName { get; set; }
        public string ProductSKU { get; set; }
    }


ConditionalAttribute

Namespace: System.Diagnostics

Usage: This is a great attribute to use with code that you only want to run in debug mode. what it does is pretty much is that the body of any method that has the conditional attribute is not compiled when that condition is false.

 

Sample:

        [Conditional("CONDITION1")]
        public static void Method1(int x)
        {
            Console.WriteLine("CONDITION1 is defined");
        }


DebuggerStepThroughAttribute

Namespace: System.Diagnostics

Usage: This attribute is great to skip through methods or properties that only have getters and setters defined.

Sample:

        [DebuggerStepThrough()]
        public virtual int AddressId
        {
            get { return _AddressId;}
           
            set
           
{
               
                _AddressId = value;
               
                OnPropertyChanged("AddressId");
               
            }
           
        }


FlagAttribute

Namespace: System

Usage: Indicates that an enumeration can be treated as a bit field; that is, a set of flags.

Sample:

[Flags] public enum MyColors : short
   
{
        Black = 0,
        Red = 1,
        Green = 2,
        Blue = 4
    }


XmlIgnoreAttribute

Namespace: System.Xml.Serialization

Usage: Tells the the xml serializer to ignore a certain field.

Sample:

public class Group
{
   // The XmlSerializer ignores this field.
  
[XmlIgnore]
   public string Comment;

   // The XmlSerializer serializes this field.
  
public string GroupName;
}

 

Hope this helps.

Haitm

13 Comments

would love to hear if you have any other attributes that you would like to add to the list

Actually, ConditionalAttribute is even more cool. Not just it makes compiler omit the body of the method, but also the very invocation of the method. So if you have a method like LogDebug(string) and you call it like LogDebug(”blah = ” + obj.Prop1) the entire line won’t be compiled, and the obj.Prop1 will not even get evaluated!

Also Obsolete attribute is useful when you want to mark outdated functions and classes.

DescriptionAttribute is another one I use fairly often to put expanded description text against enum values. Although it can be applied to anything.

[...] Posted by aboutdev on December 19, 2009 I recently came across this gem on Hatim’s Development Blog.  It’s a post titled 5 Very Useful C# Attributes. [...]

FlagsAttribute isn’t required to use an enumeration like a bitfield. You can use any enum like a bitfield whose constants are set in powers of two. The primary purpose of this flag is to indicate to users of your code that your intent is to have it used like a bitfield, and to overrite the ToString() method to provide proper output. See http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx for details.

Flags does not simply show the user of the code that this is intended as a flag enumeration, it also tells the compiler.
While it is true that any enumeration can be used as a bitfield if it is not marked with the Flags attribute you must first cast to the underlying type that supports the bitwise operation.

Right on the money Robert. Thanks for the explanation

They are not necessarily C# attributes, they can be used in VB.NET and other .NET languages as well.

But the list is great and I would add this one as well

It can generate either a warning or an error when using some old/obsolete function

[...] 5 Very Useful C# Attributes Possibly related posts: (automatically generated)Ooooh! I am so “borrowing” this!PTE5013 Foundations of Educational Research – GreenSites I should keep. [...]

[...] 5 Very Useful C# Attributes – Great list of attributes. [...]

[...] http://hatim.indexdev.net/2009/12/17/5-very-useful-c-attributes/ Posted: Jan 11 2010, 10:54 AM by Frolov Yevgeni | with no comments ????:DEV, C#, Attributes, .NET [...]

[...] more: Hatim’s Development Blog Possibly related posts: (automatically generated)P/Invoke Interop AssistantC# AttributesList all [...]

Leave a Comment