How to mark a method as Obsolete or Deprecated in C# ?

How can you mark a function or a method as deprecated in c# ? . You can do it in c# using the obsolete attribute .

For Example :

public class Employee
{

        public string Name { get; set; }

        public void SetEmployeeName(string name)
        {
            Name = name;

        }

}

If you need to mark the method SetEmployeeName as deprecated , then , just include the obsolete attrbute before the function like this

public class Employee
{
    public string Name { get; set; }

    [ Obsolete  ]
    public void SetEmployeeName(string name)
    {
        Name = name;

    }

}

When you call the SetEmployeeName , it should only show a warning stating the method is obsolete .

You can add an description for the function too via

    [  Obsolete("SetEmployeeName is deprecated,Use a different method instead", true) ]  ] 
public void SetEmployeeName(string name)
{

    Name = name;

}

The second parameter(true) defines to throw compiler error when this function is called .

Reference

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

2 Comments

  1. Mike February 7, 2011 Reply
  2. How to mark a method as Obsolete or Deprecated in C# | ProgramInDotnet August 8, 2011 Reply

Leave a Reply


x

Interesting BlogPosts to read

How to get an Enumerator from range of elements from ArrayList in C# ?
The code snippet shown in this blog post demonstrates how you can get the enumeraor from a range of ...
How to check for the column name in an SqlDataReader Object in C# ?
When working in ADO.NET for data access , there are times when you want to check to see if a column ...
How to convert Array to List in C#?
Want to convert array of strings to List of strings in C# ? . Below is a sample code to illustrate t...