Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What .Net attributes do people apply to their code? [duplicate]

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.

like image 537
Nanook Avatar asked Jan 16 '10 11:01

Nanook


3 Answers

I just found this resources:

  • Most Useful Attributes in C#
  • 5 Very Useful C# Attributes

 

// 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() { }
like image 57
Rubens Farias Avatar answered Nov 16 '22 01:11

Rubens Farias


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

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

Shouldn't this be community-wiki?

like image 25
nothrow Avatar answered Nov 15 '22 23:11

nothrow


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.

like image 31
Davy Landman Avatar answered Nov 16 '22 00:11

Davy Landman