ObsoleteAttribute Class

Definition

Marks the program elements that are no longer in use. This class cannot be inherited.

[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct | System.AttributeTargets.Enum | System.AttributeTargets.Constructor | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Field | System.AttributeTargets.Event | System.AttributeTargets.Interface | System.AttributeTargets.Delegate, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ObsoleteAttribute : Attribute
Inheritance
ObsoleteAttribute
Attributes

Examples

The following example defines a class that contains a property and a method that are marked with the ObsoleteAttribute attribute. Accessing the value of the OldProperty property in code generates a compiler warning, but calling the CallOldMethod method generates a compiler error. The example also shows the output that results when you attempt to compile the source code.

using System;
using System.Reflection;

public class Example
{
   // Mark OldProperty As Obsolete.
   [ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", false)] 
   public static string OldProperty
   { get { return "The old property value."; } }
   
   public static string NewProperty
   { get { return "The new property value."; } }

   // Mark CallOldMethod As Obsolete.
   [ObsoleteAttribute("This method is obsolete. Call CallNewMethod instead.", true)] 
   public static string CallOldMethod()
   {
      return "You have called CallOldMethod.";
   }
      
   public static string CallNewMethod() 
   {   
      return "You have called CallNewMethod.";
   }   

   public static void Main()
   {                 
      Console.WriteLine(OldProperty);
      Console.WriteLine();
      Console.WriteLine(CallOldMethod());
   } 
}
// The attempt to compile this example produces output like the following output:
//    Example.cs(31,25): error CS0619: 'Example.CallOldMethod()' is obsolete: 
//            'This method is obsolete. Call CallNewMethod instead.'
//    Example.cs(29,25): warning CS0618: 'Example.OldProperty' is obsolete: 
//            'This property is obsolete. Use NewProperty instead.'
Imports System.Reflection

Public Module Example
   ' Mark OldProperty As Obsolete.
   <ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", False)> 
   Public ReadOnly Property OldProperty As String
      Get
         Return "The old property value."
      End Get
   End Property
   
   Public ReadOnly Property NewProperty As String
      Get
         Return "The new property value."
      End Get
   End Property
   
   ' Mark OldMethod As Obsolete.
   <ObsoleteAttribute("This method is obsolete. Call CallNewMethod instead.", True)> 
   Public Function CallOldMethod() As String
      Return "You have called CallOldMethod."
   End Function
      
   Public Function CallNewMethod() As String   
      Return "You have called NewMethod."
   End Function   
   
   Public Sub Main()
      Console.WriteLine(OldProperty)
      Console.WriteLine()
      Console.WriteLine(CallOldMethod())
   End Sub  
End Module
'  The attempt to compile this example produces output like the following:
'    Example.vb(30) : warning BC40000: 'Public ReadOnly Property OldProperty As String' is obsolete: 
'                     'This property is obsolete. Use NewProperty instead.'.
'    
'          Console.WriteLine(OldProperty)
'                            ~~~~~~~~~~~
'    Example.vb(32) : error BC30668: 'Public Function CallOldMethod() As String' is obsolete: 
'                     'This method is obsolete. Call CallNewMethod instead.'.
'    
'          Console.WriteLine(CallOldMethod())
'                            ~~~~~~~~~~~~~

Remarks

ObsoleteAttribute is applicable to all program elements except assemblies, modules, parameters, and return values. Marking an element as obsolete informs users that the element will be removed in future versions of the product.

The ObsoleteAttribute class includes two properties:

  • Message. The string assigned to the Message property is emitted by the compiler when the attribute target is used in code. The string should note that the attribute target is obsolete and, if possible, provide some workaround or programmatic alternative.

  • IsError. This is a Boolean value that indicates to the compiler whether using the ObsoleteAttribute attribute should cause it to emit an error (IsError is true) or a warning (IsError is false).

For more information about using attributes, see Attributes.

Windows 8.x Store Apps

When you create a Windows Metadata library (.winmd file), the ObsoleteAttribute is exported as both the ObsoleteAttribute attribute and the Windows.Foundation.Metadata.DeprecatedAttribute attribute if only the ObsoleteAttribute is present in source code. The ObsoleteAttribute is transformed to the DeprecatedAttribute as follows:

  • If the message and error arguments are both present, message is assigned to the DeprecatedAttribute message argument. An error value of true maps to DeprecationType.Remove, and an error value of false maps to DeprecationType.Deprecate.

  • If the message argument is not supplied in the ObsoleteAttribute, its default value in the DeprecatedAttribute is "element_name is deprecated", where element_name is the name of the target program element to which the attribute is applied.

  • If the error argument is not present in the ObsoleteAttribute, its default value in the DeprecatedAttribute is DeprecationType.Deprecate.

Important

Directly applying the Windows.Foundation.Metadata.DeprecatedAttribute attribute to managed code is not recommended, because this export occurs automatically.

Constructors

ObsoleteAttribute()

Initializes a new instance of the ObsoleteAttribute class with default properties.

ObsoleteAttribute(String)

Initializes a new instance of the ObsoleteAttribute class with a specified workaround message.

ObsoleteAttribute(String, Boolean)

Initializes a new instance of the ObsoleteAttribute class with a workaround message and a Boolean value indicating whether the obsolete element usage is considered an error.

Properties

IsError

Gets a Boolean value indicating whether the compiler will treat usage of the obsolete program element as an error.

Message

Gets the workaround message, including a description of the alternative program elements.

TypeId Inherited from Attribute

Methods

Equals(Object) Inherited from Attribute
Equals(Object, Object) Inherited from Object
GetCustomAttribute(Assembly, Type) Inherited from Attribute
GetCustomAttribute(Assembly, Type, Boolean) Inherited from Attribute
GetCustomAttribute(MemberInfo, Type) Inherited from Attribute
GetCustomAttribute(MemberInfo, Type, Boolean) Inherited from Attribute
GetCustomAttribute(Module, Type) Inherited from Attribute
GetCustomAttribute(Module, Type, Boolean) Inherited from Attribute
GetCustomAttribute(ParameterInfo, Type) Inherited from Attribute
GetCustomAttribute(ParameterInfo, Type, Boolean) Inherited from Attribute
GetCustomAttributes(Assembly) Inherited from Attribute
GetCustomAttributes(Assembly, Boolean) Inherited from Attribute
GetCustomAttributes(Assembly, Type) Inherited from Attribute
GetCustomAttributes(Assembly, Type, Boolean) Inherited from Attribute
GetCustomAttributes(MemberInfo) Inherited from Attribute
GetCustomAttributes(MemberInfo, Boolean) Inherited from Attribute
GetCustomAttributes(MemberInfo, Type) Inherited from Attribute
GetCustomAttributes(MemberInfo, Type, Boolean) Inherited from Attribute
GetCustomAttributes(Module) Inherited from Attribute
GetCustomAttributes(Module, Boolean) Inherited from Attribute
GetCustomAttributes(Module, Type) Inherited from Attribute
GetCustomAttributes(Module, Type, Boolean) Inherited from Attribute
GetCustomAttributes(ParameterInfo) Inherited from Attribute
GetCustomAttributes(ParameterInfo, Boolean) Inherited from Attribute
GetCustomAttributes(ParameterInfo, Type) Inherited from Attribute
GetCustomAttributes(ParameterInfo, Type, Boolean) Inherited from Attribute
GetHashCode() Inherited from Attribute
GetType() Inherited from Object
IsDefaultAttribute() Inherited from Attribute
IsDefined(Assembly, Type) Inherited from Attribute
IsDefined(Assembly, Type, Boolean) Inherited from Attribute
IsDefined(MemberInfo, Type) Inherited from Attribute
IsDefined(MemberInfo, Type, Boolean) Inherited from Attribute
IsDefined(Module, Type) Inherited from Attribute
IsDefined(Module, Type, Boolean) Inherited from Attribute
IsDefined(ParameterInfo, Type) Inherited from Attribute
IsDefined(ParameterInfo, Type, Boolean) Inherited from Attribute
Match(Object) Inherited from Attribute
MemberwiseClone() Inherited from Object
ReferenceEquals(Object, Object) Inherited from Object
ToString() Inherited from Object
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) Inherited from Attribute
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) Inherited from Attribute
_Attribute.GetTypeInfoCount(UInt32) Inherited from Attribute
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) Inherited from Attribute

Applies to

See Also