System.ObsoleteAttribute Class

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

See Also: ObsoleteAttribute Members

Syntax

[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 | System.AttributeTargets.All, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ObsoleteAttribute : Attribute

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:

For more information about using attributes, see [<topic://cpconExtendingMetadataUsingAttributes>].

win8_appname_long Apps

When you create a Windows Metadata library (.winmd file), the ObsoleteAttribute is exported as both the ObsoleteAttribute attribute and the tp://msdn.microsoft.com/library/windows/apps/windows.foundation.metadata.deprecatedattribute.aspx attribute if only the ObsoleteAttribute is present in source code. The ObsoleteAttribute is transformed to the DeprecatedAttribute as follows:

Note:

Directly applying the tp://msdn.microsoft.com/library/windows/apps/windows.foundation.metadata.deprecatedattribute.aspx attribute to managed code is not recommended, because this export occurs automatically.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Example

The following example demonstrates the usage of ObsoleteAttribute to generate a compile-time warning.

C# Example

   
using System;

public class ObsoleteAttributeExample {

  [ObsoleteAttribute("OldMethod is being removed: use NewMethod in future versions.")]
  public static void OldMethod() {

    //Execute some code here
  }

  public static void Main() { 

    OldMethod();
  }
}

An example compile-time result is

ObsoleteAttributeExample.cs(8,4): warning CS0618: 'ObsoleteAttributeExample.OldMethod()' is obsolete: 'OldMethod is being removed: use NewMethod in future versions.'

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0