views:

162

answers:

7

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.

+1  A: 

I usually use [Browsable(false)] and [Serializable].

[Browsable(false)] on property hides the property from PropertyGrid.

Shouldn't this be community-wiki?

Yossarian
I'm new to this site, I haven't looked at the wiki, I will now. Thanks
Nanook
A: 
[DebuggerDisplay(....)]

to define what fields of a structure or class I want to see in the debugger display.

marc_s
+1  A: 

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.

Davy Landman
+4  A: 

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() { }
Rubens Farias
+1  A: 

We have a lot of CLS compliant code and some not, so it's clearly a plus for us:

[assembly:CLSCompliant(true)]
[CLSCompliant(true)]

This helps us a lot.

mjsabby
Oh and [ComVisibleAttribute(true)]
mjsabby
A: 

Sometimes BindableAttribute is nice to influence the binding behavior of components. Maybe it is helpful to fire up Reflector and search for "Attribute" and browse a little bit. It depends on your intention which ones are useful.

Carsten
A: 

I used conditional attributes during the creation of a demo application.I made it as a full version and suppress some functionalities using these type of attributes.

Vibin Jith