Monday, March 7, 2011

Marking class or Function "Obsolete"

You might have encounter warnings from C# compiler that Class or Function is Obsolete try another function. This indication help developers while using different version of identical Functions or Classes.
You can add this indication for your Class or Function, before .net, this wasn't possible. You will able to specify warnings regarding the obsolete when you are working on upgraded version of your code.
Let us see how can we do this:



Class MyAttribute
   {
      [Obsolete()]//Compiler will prompt obsolete message while using this function
      public static void Some(string str)
      {
         Console.WriteLine(str);
      }
[Obsolete("Some() method is now Obsolete. Please use Print()")]//Customizing obsolete msg
public static void Print(string str)
      {
         Console.WriteLine(str);
      }

      static void Main()
      {
         Some("Hello");
         Console.ReadLine();
      }
   }

No comments:

Post a Comment