Attributes in C#

What is Attribute?

Attribute is a class used to give additional declarative information for the class, method, property, indexer, etc. By using attribute you can identify the behavior of the class, method, property, indexer, etc.

Following are the different types of attributes in C#:

1. Obsolete:

It is used to specify whether the class or method is deprecated or not. Obsolete attribute contains two arguments:

i) Message
ii)True/False

If value is false, when deprecated method is used in the program then with warning the program will execute. If value is True, when deprecated method is used in application then compilation error will be displayed.

Examples:

Example of Obsolete:

using System;

namespace Attribute
{
    class Program
    {
        [Obsolete("display1 is deprecated, use display2", false)]
        public void display1()
        {
            Console.WriteLine("Visual Studio 2008");
        }
        public void display2()
        {
            Console.WriteLine("Visual Studio 2015");
        }

        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.display1();
            Console.Read();
        }
    }
}

Output:

Obsolete

2. Conditional:

It is used to specify whether the method should be called or not. The conditional method must be a method in a class or structure declaration. A compile-time error occurs if the Conditional attribute is specified on a method in an interface declaration. The conditional method must have a return type of void. Conditional attribute is in System.Diagnostics namespace.

Example of Conditional:

#define hello
using System;
using System.Diagnostics;

namespace AttributeConditional
{
    class Program
    {
        [Conditional("hello")]
        public void print()
        {
            Console.WriteLine("Happy coding...");
        }
        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.print();
            Console.Read();
        }
    }
}

Output:

Conditional

3. WebMethod:

This attribute will be used in xml web services in ASP.Net. In xml web service if any method or function is having this attribute then the method can be exposed on web or the method will execute in any browser.

Example of WebMethod:

[WebMethod]
Public string HelloWorld()
{
    return “Hello World”;
}

4. DllImport:

DLL files are of two types: Managed dll and unmanaged dll. Old languages (such as Visual Basic 6.0, VC++, etc) and O.S. dll files are called unmanaged dll files. These will contain unmanaged code which will execute without CLR. Dot Net dll files are called managed dll files. It contains managed code which will execute through CLR. To use unmanaged dll files in managed code, DllImport attribute is used.

Example of DllImport:

using System;
using System.Runtime.InteropServices; // DllImport

class App
{
    [DllImport("lib.dll")]
    extern static int fun(int n);

    static void Main()
    {
        Console.WriteLine(fun(0));
    }
}

5. Creating user defined Attributes:

User defined attribute can be used to give information about the class or method. i.e whether the class is user defined class or abstract class, base class etc. and for method you can specify whether it is user defined method, abstract method, virtual method, etc. Attribute class in System namespace is a base class for custom attribute or user defined attribute.

Example of Creating user defined Attributes:

using System;

namespace CustomAttribute
{
    class CustomAttribute : Attribute
    {
        private string s;
        public CustomAttribute(string str)
        {
            pstr = str;
        }
        public string pstr
        {
            get
            {
                return s;
            }
            set
            {
                s = value;
            }
        }

    }
    class MyClass
    {
        [Custom("User defined method")]
        public void getString()
        {
            Console.WriteLine("VS.Net 2015");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();
            obj.getString();
            Console.Read();
        }
    }
}

Output:

Custom

In this way, we learned attributes in C# and some basic programs which will help beginners to understand attributes.

You may also be interested in...

Happy Coding!

Article By Kamlesh Bhor

Feel free to comment below about this article.

Discuss about post

Subscribe to my weekly newsletter