public sealed class ObsoleteAttribute : Attribute
Object
Attribute
ObsoleteAttribute
mscorlib
BCL
Indicates that the target of the current attribute will be removed in future versions of the assembly in which the target is contained.
[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 isfalse
, 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 .
]
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.'
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)
System Namespace
ObsoleteAttribute Constructors
ObsoleteAttribute() Constructor
ObsoleteAttribute(System.String) Constructor
ObsoleteAttribute(System.String, bool) Constructor
ObsoleteAttribute Properties
ObsoleteAttribute.IsError Property
ObsoleteAttribute.Message Property
public ObsoleteAttribute();
Constructs and initializes a new instance of the ObsoleteAttribute class.
This constructor is equivalent to ObsoleteAttribute(null
,false
). The compiler does not treat an item with this attribute as an error.
System.ObsoleteAttribute Class, System Namespace
public ObsoleteAttribute(string message);
Constructs and initializes a new instance of the ObsoleteAttribute class with the specified String that contains suggested workarounds.
- message
- The String that contains suggested workarounds.
This constructor is equivalent to ObsoleteAttribute (message ,false
). The compiler does not treat an item with this attribute as an error.
System.ObsoleteAttribute Class, System Namespace
public ObsoleteAttribute(string message, bool error);
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.
- 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.
Respectively, the System.ObsoleteAttribute.Message property and the System.ObsoleteAttribute.IsError property of the new instance are initialized as message and error.
System.ObsoleteAttribute Class, System Namespace
public bool IsError { get; }
Gets a Boolean that indicates whether the compiler treats usage of the target of the current instance as an error.
true
if the compiler treats usage of the target of the current instance as an error; otherwise,false
.
This property is read-only.The default value of this property is
false
.
System.ObsoleteAttribute Class, System Namespace
public string Message { get; }
Gets a String that contains suggested workarounds for the target of the current instance.
A String that contains suggested workarounds for the target of the current instance.
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
.
System.ObsoleteAttribute Class, System Namespace