Information Technology Reference
In-Depth Information
A Quick Preview
As a quick preview of attributes, take a look at the following code. In class
Program
, a predefined
attribute named
Obsolete
is applied to method
PrintOut
. This attribute declares that the
method is obsolete and gives information about what method should be used in its place.
using System;
using System.Diagnostics;
namespace AttrObs
{
class Program
Apply attribute.
{
↓
[Obsolete("Use method SuperPrintOut")] // Apply attribute to method.
static void PrintOut(string str)
{
Console.WriteLine(str);
}
static void Main(string[] args)
{
PrintOut("Start of Main"); // Invoke obsolete method.
}
}
}
Notice that method
Main
calls
PrintOut
, even though it is marked as obsolete. In spite of
this, the code compiles and runs fine, and produces the following output when executed.
Start of Main
But during compilation, the compiler produces the following warning message to inform
you that you are using an obsolete construct.
AttrObs.Program.PrintOut(string)' is obsolete: 'Use method SuperPrintOut