tags:

views:

70

answers:

2

I do not understand how the DefaultProperty Metadata tag work or what it signifies. I've read the MSDN and went through the sample but I find it confusing.

DefaultPropertyAttribute Class

I've read a few blogs and they seem to refer to the indexers. I'm not sure why you would want metadata for your properties? I am coming from a Java background, perhaps a Java analogy would help.

[DefaultProperty("Value")]
    public abstract class FOO<T> : ANY, IBAR<T>
    {
        public FOO() { }
        public FOO(T value) { this.Value = value; }
        public virtual T Value { get; set; }
    }

Follow up: Property Grid

+1  A: 

This is used for property grids.

When you select a component in the designer, it looks for a DefaultProperty attribute and selects that property by default.

You can safely ignore it.

The DefaultEvent attribute is similar.

SLaks
So its similar to Java's Annotations for suppressing warnings or overriding? It's just a message to the IDE?
Shiftbit
@Shiftbit: It's like Java's annotations, and usually used by the IDE, but you can use it at runtime as well. Anything can use this. This specific one is used to show which property is the "default" on in a property grid (The "Properties Window" in the IDE)
Reed Copsey
Yes. It's actually used by the designer architecture in System.ComponentModel (not the IDE), but yes.
SLaks
+1  A: 

Attributes are metadata, like the Java annotations. They're not used by the class to which they are applied, but by other classes, or the IDE designer for example.

TskTsk
There's no compiler rule saying metadata can't be used by the class/method to which they are applied.
dbkk
True, it's just not the most common use.
TskTsk