Possible Duplicate:
Most Useful Attributes in C#
I always feel that I am missing functionality that can be gained in .Net by simply applying attributes to classes, methods, properties etc. It doesn't help that intellisense cannot display all appropriate attributes as they can normally be applied in a wide range of scenarios.
Here's a couple of the attributes I like to use:
[DebuggerHidden] - placing this over methods prevents the Visual Studio debugger from stepping in to code. This is useful if you have an event that continually fires and interupts your debugging.
[EditorBrowsable(EditorBrowsableState.Never)] - Hide a method from intellisense. I don't use this often, but it's handy when building reusable components and you want to hide some test or debug methods.
I'd like to see what others are using and what tips people have.
I just found this resources:
// The DebuggerDisplayAttribute can be a sweet shortcut to avoid expanding
// the object to get to the value of a given property when debugging.
[DebuggerDisplay("ProductName = {ProductName},ProductSKU= {ProductSKU}")]
public class Product
{
public string ProductName { get; set; }
public string ProductSKU { get; set; }
}
// This attribute is great to skip through methods or properties
// that only have getters and setters defined.
[DebuggerStepThrough()]
public virtual int AddressId
{
get { return _AddressId;}
set
{
_AddressId = value;
OnPropertyChanged("AddressId");
}
}
// The method below is marked with the ObsoleteAttribute.
// Any code that attempts to call this method will get a warning.
[Obsolete("Do not call this method.")]
private static void SomeDeprecatedMethod() { }
// similar to using a combination of the DebuggerHidden attribute, which hides
// the code from the debugger, and the DebuggerStepThrough attribute, which tells
// the debugger to step through, rather than into, the code it is applied to.
[DebuggerNonUserCode()]
private static void SomeInternalCode() { }
I usually use [Browsable(false)] and [Serializable].
[Browsable(false)] on property hides the property from PropertyGrid.
Shouldn't this be community-wiki?
I realy like the DebuggerDisplay:
[DebuggerDisplay("Count = {count}")]
class MyHashtable
{
public int count = 4;
}
It will instruct VS what to display when hovering over the item.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With