Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 52 - C# Tutorial - Attributes in C#

In this part we will learn
1. The purpose of attributes
2. Using an attribute
3. Customizing attribute using parameters


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


Part 52 - C# Tutorial - Attributes in C#



There are several Pre-defined Attributes provided by .NET. It is also possible to create your own Custom Attributes. Creating custom attributes is beyond the scope of this article.


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


Example program using pre defined Obsolete attribute:
Obsolete attribute can be used with types or type members that are obsolete (Outdated). If a developer uses a type or a type member that is decorated with obsolete attribute, the compiler issues a warning or an error depending on how the attribute is configured.


In this sample program, Add(int FirstNumber, int SecondNumber) method is decorated with [Obsolete] attribute. If you compile this program, in the output window you will see a warning message (Compile complete -- 0 errors, 1 warnings). Also, visual studio, shows a green squiggly line under the  Add(int FirstNumber, int SecondNumber) method. If you hover the mouse over the squiggly line, you should see the warning message.


Note: If you don't see the warning message (Compile complete -- 0 errors, 1 warnings), rebuild the soultion.


using System;
using System.Collections.Generic;
public class MainClass
{
    private static void Main()
    {
        Calculator.Add(10, 15);
    }
}


public class Calculator
{
    [Obsolete]
    public static int Add(int FirstNumber, int SecondNumber)
    {
        return FirstNumber + SecondNumber;
    }
    public static int Add(List<int> Numbers)
    {
        int Sum = 0;
        foreach (int Number in Numbers)
        {
            Sum = Sum + Number;
        }
        return Sum;
    }
}


The warning message says 'Calculator.Add(int, int)' is obsolete. However, this message is not completely useful, because it says 'Calculator.Add(int, int)' is obsolete, but not tell us which other method should we be using instead. So this is when we can customize, the warning message using attribute parameters.


The intention of the developer of Calculator class is that, he wanted us to use Add(List<int> Numbers), instead of int Add(int FirstNumber, int SecondNumber). To communicate this message we can customize the warning message using attribute parameters as shown below. With this customization we are not only communicating that Add(int FirstNumber, int SecondNumber) method is obsolete, we are also telling to use the alternative method that is available.
[Obsolete("Use Add(List<int> Numbers) instead")]
public static int Add(int FirstNumber, int SecondNumber)


If you want to generate a compiler error instead of warning, pass true for the bool error parameter of the Obsolete attribute as shown below. Now, we can't even compile the program.
[Obsolete("Use Add(List<int> Numbers) instead", true)]
public static int Add(int FirstNumber, int SecondNumber)


Finally, If you right click on Obsolete attribute and select Go To Definition, you will see that, an attribute is nothing but a class that inherits from System.Attribute base class.

8 comments:

  1. hey venket ur video lecture is awesum...venket pls i have a request for u ..pls discuss generic and collection and threads in details vedio..."what are generic is allright ..but need more details..pls pls pls ..that would be very very usefull for me ..and guys like me ..pls pls pls

    ReplyDelete
    Replies
    1. Hi Bhabesh,

      Please find the video links for Generics and Generic Collections. Please let me know if you need anything else.

      Generics

      Generic Collections

      Delete
  2. Hi Venket Thanks so much for these videos.
    And my request is add video about normalization in sql server video please

    ReplyDelete
    Replies
    1. Hi Ranjan,

      I will surely record a video on Normalization and Denormalization concepts. If you want to receive email alerts, when I upload new videos, please subscribe to my youtube channel.

      Delete
  3. Hi Venkat,
    i have gain so much confidence after watching videos of c# please discuss exception handling concepts .

    Thanks,
    Asif.

    ReplyDelete
    Replies
    1. Exception handling in C#, is already discussed. Please check parts 40, 41, 42, 43, and 44. If you are referring to SQL server exception handling, I will do it as soon as I can.

      Delete
  4. Hi Venkat,
    Can you make video on creating & applying Custom Attributes in C#. Thanks in advance,

    ReplyDelete
  5. Hi Venkat thank u for ur videos..it has helped e a lot, can you please post a video on page lifecycle in asp.net in detail. I have seen ur video on pagelifecycle but can you please make it more detailed

    ReplyDelete

It would be great if you can help share these free resources