ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Advanced Programming
    CSI-415
    Progress0 / 55 topics
    Topics
    1. Visual Programming Basics2. Introduction to Events3. Fundamentals of Event-Driven Programming4. Message Handling5. User Interfaces6. Graphics Device Interface7. Painting and Drawing8. Windows Management9. Input Devices10. Resources11. String and Menu Resource12. Dialogs and Windows Controls13. Common Controls14. Dynamic Link Libraries (DLLs)15. Threads and Synchronization16. Network Programming17. Building Class Libraries at the Command Line18. Class Libraries19. Using References20. Assemblies21. Private Assembly Deployment22. Shared Assembly Deployment23. Configuration Overview24. Configuration Files25. Programmatic Access to Configuration26. Using SDK Tools for Signing and Deployment27. Metadata28. Reflection29. Late Binding30. Directories and Files31. Serialization32. Attributes33. Memory Management and Garbage Collection34. Threading and Synchronization35. Asynchronous Delegates36. Application Domains37. Marshal by Value38. Marshal by Reference39. Authentication and Authorization40. Configuring Security41. Code Access Security42. Code Groups43. Evidence44. Permissions45. Role-Based Security46. Principals and Identities47. Using Data Readers48. Using Data Sets49. Interacting with XML Data50. Tracing Event Logs51. Using the Boolean Switch and Trace Switch Classes52. Print Debugging Information with the Debug Class53. Instrumenting Release Builds with the Trace Class54. Using Listeners55. Implementing Custom Listeners
    CSI-415›Attributes
    Advanced ProgrammingTopic 32 of 55

    Attributes

    7 minread
    1,222words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,222
      Code examples0
      DifficultyIntermediate