Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# to Java: What is a [DefaultProperty("value")]?

Tags:

c#

metadata

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


2 Answers

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.

like image 179
SLaks Avatar answered May 14 '26 03:05

SLaks


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.

like image 26
jvilalta Avatar answered May 14 '26 03:05

jvilalta