14 Jun

Most Useful and Most Commonly Used Attributes in C#

This example shows the attributes that are most commonly used and most useful in C#.

        //mark types and members of types that should no longer be used
        [Obsolete("Don't use")]

        //Specifies the display name for a property, event, or public void method which takes no arguments.
        [DisplayName("Web Site Url")]

        //Specifies a description for a property or event.
        [Description("Web site url description")]

        //Specifies the default value for a property.
        [DefaultValue("https://csharpexamples.com")]

        //indicates that a class can be serialized. This class cannot be inherited.
        [Serializable]

        //controls XML serialization of the attribute target as an XML root element.
        [XmlRoot]

        //indicates that a public field or property represents an XML element when the XmlSerializer serializes or deserializes the object that contains it.
        [XmlElement]

        //specifies that the XmlSerializer must serialize the class member as an XML attribute.
        [XmlAttribute]

        //instructs the Serialize method of the XmlSerializer not to serialize the public field or public read/write property value.
        [XmlIgnore]

        //controls accessibility of an individual managed type or member, or of all types within an assembly, to COM.
        [ComVisible(false)]

        //which allows you to hide properties
        [Browsable(false)]

        //tells the designer to expand the properties which are classes 
        [TypeConverter(typeof(ExpandableObjectConverter))]

Leave a Reply

Your email address will not be published. Required fields are marked *