System.ObsoleteAttribute Class

public sealed class ObsoleteAttribute : Attribute

Base Types

Object
  Attribute
    ObsoleteAttribute

Assembly

mscorlib

Library

BCL

Summary

Indicates that the target of the current attribute will be removed in future versions of the assembly in which the target is contained.

Description

[Note: Marking an item as obsolete provides consumers of that item the information that the item will be not be available in future versions of the assembly in which it is contained. A ObsoleteAttribute has a System.ObsoleteAttribute.Message property that can be used to suggest alternative ways of obtaining the functionality provided by the item, i.e. a workaround. This class also has a System.ObsoleteAttribute.IsError property that designates whether a compiler will treat usage of the obsolete item as an error. If this property is false , the compiler will issue a warning if the obsolete item is used and the compiler supports the generation of such warnings.

This attribute can be applied to any valid attribute target except assemblies, parameters, and return values. For a complete list of valid attribute targets, see AttributeTargets .

]

Example

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

   
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.'

Attributes

AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple=false, Inherited=false)

See Also

System Namespace

Members

ObsoleteAttribute Constructors

ObsoleteAttribute() Constructor
ObsoleteAttribute(System.String) Constructor
ObsoleteAttribute(System.String, bool) Constructor

ObsoleteAttribute Properties

ObsoleteAttribute.IsError Property
ObsoleteAttribute.Message Property


ObsoleteAttribute() Constructor

public ObsoleteAttribute();

Summary

Constructs and initializes a new instance of the ObsoleteAttribute class.

Description

This constructor is equivalent to ObsoleteAttribute(null , false ). The compiler does not treat an item with this attribute as an error.

See Also

System.ObsoleteAttribute Class, System Namespace

ObsoleteAttribute(System.String) Constructor

public ObsoleteAttribute(string message);

Summary

Constructs and initializes a new instance of the ObsoleteAttribute class with the specified String that contains suggested workarounds.

Parameters

message
The String that contains suggested workarounds.

Description

This constructor is equivalent to ObsoleteAttribute (message , false ). The compiler does not treat an item with this attribute as an error.

See Also

System.ObsoleteAttribute Class, System Namespace

ObsoleteAttribute(System.String, bool) Constructor

public ObsoleteAttribute(string message, bool error);

Summary

Constructs and initializes a new instance of the ObsoleteAttribute class with a String that contains suggested workarounds and a Boolean that indicates whether the compiler treats usage of the target of the current instance as an error.

Parameters

message
A String that contains suggested workarounds.
error
A Boolean that indicates whether the compiler treats usage of the target of the current instance as an error.

Description

Respectively, the System.ObsoleteAttribute.Message property and the System.ObsoleteAttribute.IsError property of the new instance are initialized as message and error.

See Also

System.ObsoleteAttribute Class, System Namespace

ObsoleteAttribute.IsError Property

public bool IsError { get; }

Summary

Gets a Boolean that indicates whether the compiler treats usage of the target of the current instance as an error.

Property Value

true if the compiler treats usage of the target of the current instance as an error; otherwise, false .

Description

This property is read-only.

The default value of this property is false .

See Also

System.ObsoleteAttribute Class, System Namespace

ObsoleteAttribute.Message Property

public string Message { get; }

Summary

Gets a String that contains suggested workarounds for the target of the current instance.

Property Value

A String that contains suggested workarounds for the target of the current instance.

Description

This property is read-only.

The current instance contains a suggested workaround message if and only if such a message was specified when the current instance was constructed. If no workaround was specified for the current instance, the value of this property is null .

See Also

System.ObsoleteAttribute Class, System Namespace