ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCSI-415Attributes
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CSI-415

    Advanced Programming

    Progress0 / 55 done
    Topics55
    1Visual Programming Basics2Introduction to Events3Fundamentals of Event-Driven Programming4Message Handling5User Interfaces6Graphics Device Interface7Painting and Drawing8Windows Management9Input Devices10Resources11String and Menu Resource12Dialogs and Windows Controls13Common Controls14Dynamic Link Libraries (DLLs)15Threads and Synchronization16Network Programming17Building Class Libraries at the Command Line18Class Libraries19Using References20Assemblies21Private Assembly Deployment22Shared Assembly Deployment23Configuration Overview24Configuration Files25Programmatic Access to Configuration26Using SDK Tools for Signing and Deployment27Metadata28Reflection29Late Binding30Directories and Files31Serialization32Attributes33Memory Management and Garbage Collection34Threading and Synchronization35Asynchronous Delegates36Application Domains37Marshal by Value38Marshal by Reference39Authentication and Authorization40Configuring Security41Code Access Security42Code Groups43Evidence44Permissions45Role-Based Security46Principals and Identities47Using Data Readers48Using Data Sets49Interacting with XML Data50Tracing Event Logs51Using the Boolean Switch and Trace Switch Classes52Print Debugging Information with the Debug Class53Instrumenting Release Builds with the Trace Class54Using Listeners55Implementing Custom Listeners
    7 min read
    Topic 32 of 55
    Topic Study Note Illustration
    32

    Attributes

    In C#, attributes are a powerful feature that allows you to add metadata to your code elements (such as classes, methods, properties, and fields) at compile time....

    Attributes in C#

    In C#, attributes are a powerful feature that allows you to add metadata to your code elements (such as classes, methods, properties, and fields) at compile time. These attributes are used to provide additional information or instructions to the .NET runtime, compilers, or other tools that process your code. Attributes do not change the execution of the program directly but can influence behavior in various ways, such as during serialization, validation, or reflection.

    1. What are Attributes?

    Attributes are special types that are used to annotate program entities with metadata. They are applied to various program elements like classes, methods, properties, fields, parameters, and assemblies. Attributes are enclosed in square brackets ([]) and placed directly before the entity they annotate.

    For example:

    [Obsolete("This method is deprecated.")]
    public void OldMethod() 
    {
        // Code
    }
    

    In this example, the Obsolete attribute indicates that the OldMethod is deprecated.

    2. How to Define and Apply Attributes

    • Defining an Attribute: You can create your own custom attribute by deriving a class from System.Attribute. Custom attributes can have constructors and properties, just like any other class in C#.

    • Applying an Attribute: You apply an attribute by placing it above the entity you want to annotate. Some attributes, like Obsolete and Serializable, come predefined with .NET, but you can also define your own.

    Example of Creating a Custom Attribute

    using System;
    
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] // Apply to classes or methods
    public class DeveloperInfoAttribute : Attribute
    {
        public string Developer { get; }
        public string Version { get; }
    
        // Constructor for initializing the custom attribute
        public DeveloperInfoAttribute(string developer, string version)
        {
            Developer = developer;
            Version = version;
        }
    }
    
    // Applying the custom attribute to a class
    [DeveloperInfo("John Doe", "1.0")]
    public class MyClass
    {
        public void MyMethod()
        {
            Console.WriteLine("This is my method.");
        }
    }
    

    In this example:

    • DeveloperInfoAttribute is a custom attribute that holds the developer's name and the version of a class or method.
    • The attribute is applied to the MyClass class with the developer and version information.

    3. Commonly Used Built-In Attributes

    C# comes with a rich set of built-in attributes that are used for a variety of purposes, such as marking classes for serialization, validation, or indicating method behavior.

    A. [Obsolete] Attribute

    The Obsolete attribute is used to mark methods or classes as outdated, indicating that they should no longer be used.

    [Obsolete("Use NewMethod instead.")]
    public void OldMethod()
    {
        // Code
    }
    

    The [Obsolete] attribute will generate a compile-time warning or error if the deprecated method is used.

    B. [Serializable] Attribute

    The Serializable attribute marks a class as serializable, meaning its instances can be converted to a binary format (for storage or transmission).

    [Serializable]
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    

    Without this attribute, the class cannot be serialized using methods like BinaryFormatter.

    C. [NonSerialized] Attribute

    The NonSerialized attribute marks a field in a Serializable class that should not be serialized.

    [Serializable]
    public class Person
    {
        public string Name { get; set; }
    
        [NonSerialized]
        public int TempData;  // This field will not be serialized
    }
    

    D. [DebuggerStepThrough] Attribute

    The DebuggerStepThrough attribute tells the debugger to step through a method without breaking when debugging.

    [DebuggerStepThrough]
    public void DoSomeWork()
    {
        // Some complex logic
    }
    

    E. [Conditional] Attribute

    The Conditional attribute is used to include or exclude method calls based on a preprocessor directive.

    [Conditional("DEBUG")]
    public void DebugOnlyMethod()
    {
        Console.WriteLine("This method is only called in debug mode.");
    }
    

    The method is only called if the "DEBUG" symbol is defined (usually during debugging).

    F. [DllImport] Attribute

    The DllImport attribute is used to call unmanaged functions from external DLLs, typically used in interop scenarios with native code.

    using System;
    using System.Runtime.InteropServices;
    
    class Program
    {
        [DllImport("user32.dll")]
        public static extern void MessageBox(IntPtr hWnd, String text, String caption, uint type);
    
        static void Main()
        {
            MessageBox(IntPtr.Zero, "Hello from Windows", "Message", 0);
        }
    }
    

    4. Attributes and Reflection

    You can access and work with attributes programmatically using reflection. Reflection allows you to examine the metadata of types and their members at runtime. By using reflection, you can read custom attributes applied to classes, methods, and other program elements.

    Example of Reading an Attribute Using Reflection

    using System;
    using System.Reflection;
    
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class DeveloperInfoAttribute : Attribute
    {
        public string Developer { get; }
        public string Version { get; }
    
        public DeveloperInfoAttribute(string developer, string version)
        {
            Developer = developer;
            Version = version;
        }
    }
    
    [DeveloperInfo("John Doe", "1.0")]
    public class MyClass
    {
        public void Display() 
        {
            Console.WriteLine("MyClass Display method.");
        }
    }
    
    class Program
    {
        static void Main()
        {
            // Getting the type of MyClass
            Type type = typeof(MyClass);
            
            // Checking for DeveloperInfoAttribute on MyClass
            var attributes = type.GetCustomAttributes(typeof(DeveloperInfoAttribute), false);
            
            if (attributes.Length > 0)
            {
                DeveloperInfoAttribute devInfo = (DeveloperInfoAttribute)attributes[0];
                Console.WriteLine($"Developer: {devInfo.Developer}, Version: {devInfo.Version}");
            }
        }
    }
    

    In this example:

    • The DeveloperInfoAttribute is applied to the MyClass class.
    • The program uses reflection to inspect the attributes of MyClass and prints the developer name and version.

    5. AttributeUsage Attribute

    The AttributeUsage attribute is applied to custom attributes to specify where they can be used (e.g., methods, classes, properties, etc.).

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public class CustomAttribute : Attribute
    {
        public string Description { get; }
        public CustomAttribute(string description)
        {
            Description = description;
        }
    }
    
    • AttributeTargets.Class: Specifies that the attribute can be applied to classes.
    • AttributeTargets.Method: Specifies that the attribute can be applied to methods.
    • AllowMultiple = false: Indicates that the attribute can be applied only once to each class or method.

    6. Allowing Multiple Attributes

    You can allow an attribute to be applied multiple times to the same code element by setting the AllowMultiple property of AttributeUsage to true.

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class LogAttribute : Attribute
    {
        public string LogMessage { get; }
        public LogAttribute(string message)
        {
            LogMessage = message;
        }
    }
    
    public class Example
    {
        [Log("Starting Method A")]
        [Log("Method A running")]
        public void MethodA() 
        {
            Console.WriteLine("Executing Method A.");
        }
    }
    

    In this example, the Log attribute can be applied multiple times to the MethodA method.

    7. Summary

    • Attributes are used to provide metadata or additional information about code elements.
    • They are defined by creating classes that derive from System.Attribute and can be applied to classes, methods, fields, properties, and more.
    • Common attributes in .NET include [Serializable], [Obsolete], [Conditional], and [DllImport].
    • Attributes can be read and processed using reflection, allowing dynamic behavior based on the metadata.
    • The AttributeUsage attribute controls where and how an attribute can be used in the code.

    Attributes are widely used in .NET for various tasks like data serialization, method logging, validation, and configuration, enabling cleaner and more maintainable code.

    ← Previous topic 31
    Serialization
    Next topic 33 →
    Memory Management and Garbage Collection

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Advanced Programming