Menu Close

ATTRIBUTES IN C#

Attributes

Attributes allow you to add declarative information to your programs. This information can then be queried at runtime using reflection

There are several Pre-defined attributes provided by .NET.It is also possible to create your own Custom Attributes.

A few pre-defined attributes with in the .NET framework.
obsolete    – Marks types and type members outdated
WebMethod  – To expose a method as an XML Web service method
Serializable  – Indicates that a class can be serialized


It is possible to customize the attributes using parameters
An attributes is a class that inherits from SYSTEM.ATTRIBUTES base class.
————————————————————————————————————————–

using System;
using System.Collections.Generic;

namespace ConsoleApplication4
{
    public class MainClass
    {
        private static void Main()
        {
            Calculator.AddNumber(new List<int>() {10,20, 30});
           

        }
    }

    public class Calculator
    {
        [Obsolete(“Use(List<int> Numbers)Method”,true)]
        public static int AddNumber(int FirstNumber, int SecondNumber)
        {
            return FirstNumber + SecondNumber;
        }

        public static int AddNumber(List<int> Numbers)
        {
            int Sum = 0;
            foreach (int Number in Numbers)
            {
                Sum = Sum + Number;
            }
            return Sum;
        }
    }

}

Share this:
Posted in Blog

Related Posts