When do we need to use [Browsable(true)]
?
EDIT (by SLaks): He\'s asking (I assume) why one would need to pass true
as the
Probably when you want to make damn sure no one changes it :P
// I want to see this, dont change it to false or I'll hunt you down...
[Browsable(true)]
public int MyProperty {
get {
// Insert code here.
return 0;
}
set {
// Insert code here.
}
}
MSDN says it all:
Specifies whether a property or event should be displayed in a Properties window.
For example, if you're creating a User Control, you might want to decorate non-UI-related properties with [Browsable(false)]
so that they will not be available through a "Properties" window.
Additionally, it controls which properties of an object can be seen in a PropertyGrid.
As for why we can pass true
explicitly, I believe this is due to BrowsableAttributes property of a PropertyGrid
. You can set it to contain BrowsableAttribute.No, so that the property grid will display all non-browsable members.
BrowsableAttribute Class (System.ComponentModel)
The documentation states:
A visual designer typically displays in the Properties window those members that either have no browsable attribute or are marked with the BrowsableAttribute constructor's browsable parameter set to true.
[Browsable]
also defaults to true.
...so technically, you never need [Browsable(true)]
unless you want to be very explicit.