5 Very Useful C# Attributes
- 13
- Add a Comment
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.
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
Hatim
December 17th, 2009
at 12:17pm
would love to hear if you have any other attributes that you would like to add to the list
zvolkov
December 17th, 2009
at 6:17pm
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!
kiloolik
December 18th, 2009
at 4:50am
Also Obsolete attribute is useful when you want to mark outdated functions and classes.
Trev
December 18th, 2009
at 8:38am
DescriptionAttribute is another one I use fairly often to put expanded description text against enum values. Although it can be applied to anything.
Interesting Links « Aboutdev’s Weblog
December 19th, 2009
at 5:17pm
[...] 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. [...]
asd
December 20th, 2009
at 2:37pm
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.
Robert Perona
December 20th, 2009
at 8:53pm
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.
Hatim
December 21st, 2009
at 6:41pm
Right on the money Robert. Thanks for the explanation
Dejan
December 22nd, 2009
at 3:32pm
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
Some interesting articles.. « QuantuMatrix’s Weblog
December 22nd, 2009
at 5:17pm
[...] 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. [...]
Link Post 124 « Rayet’s Blog
December 24th, 2009
at 1:18am
[...] 5 Very Useful C# Attributes – Great list of attributes. [...]
So useful, but so forgotten. - Yevgeni Frolov
January 11th, 2010
at 8:54am
[...] 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 [...]
5 Very Useful C# Attributes « Jasper Blog
January 12th, 2010
at 9:35am
[...] more: Hatim’s Development Blog Possibly related posts: (automatically generated)P/Invoke Interop AssistantC# AttributesList all [...]