Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The System.ObsoleteAttribute is a predefined metadata attribute used to mark program elements that are deprecated. When the C# compiler encounters code that references an element decorated with this attribute, it evaluates the attribute’s parameters to emit either a compiler warning or a hard compiler error, effectively altering the compilation state of the referencing code (the caller).

Syntax and Constructors

The attribute can be applied using three primary constructor overloads, which dictate the severity and messaging of the compiler output.
// 1. Parameterless: Emits a default compiler warning (CS0612)
[Obsolete]
public class LegacyClass { }

// 2. Message parameter: Emits a warning with a custom message (CS0618)
[Obsolete("This method is deprecated. Call ModernMethod() instead.")]
public void DeprecatedMethod() { }

// 3. Message and Error parameters: Emits a compiler error (CS0619)
[Obsolete("This property is no longer supported.", error: true)]
public string UnsupportedProperty { get; set; }

Parameter Breakdown

  • message (string): A text string injected into the compiler output. It is technically optional but highly recommended to provide redirection to alternative APIs.
  • error (bool): A boolean flag determining the compilation behavior.
    • If false (default), the compiler emits a warning (CS0618) and compilation continues.
    • If true, the compiler emits an error (CS0619) and halts the build process.
  • DiagnosticId (string): Introduced in .NET 5 (C# 9), this named parameter allows you to assign a custom diagnostic ID to the obsolescence warning. This enables consumers to suppress specific deprecation warnings using #pragma warning disable <DiagnosticId> without suppressing all CS0618 warnings globally.
  • UrlFormat (string): Introduced in .NET 5 (C# 9), this named parameter specifies a URL format string that the compiler uses to generate a clickable link in the IDE pointing to external documentation regarding the deprecation. The format string can optionally include a {0} placeholder. If the placeholder is omitted, the compiler treats the provided string as a static URL. If the {0} placeholder is included, the compiler replaces it with the value of the DiagnosticId property. If DiagnosticId is not explicitly set, the compiler replaces {0} with the default warning code (e.g., CS0618).
Modern Syntax Example (.NET 5+):
// Using a format string with a placeholder and a custom DiagnosticId
[Obsolete("Use NewMethod instead.", DiagnosticId = "CONTOSO001", UrlFormat = "https://contoso.com/obsoletions/{0}")]
public void OldMethod() { }

// Using a static URL without a placeholder
[Obsolete("Use AnotherMethod instead.", UrlFormat = "https://contoso.com/obsoletions/another-method")]
public void AnotherOldMethod() { }

Attribute Targets

The [Obsolete] attribute is defined using a specific bitwise combination of AttributeTargets, specifically: Class | Struct | Enum | Constructor | Method | Property | Field | Event | Interface | Delegate. Because it is restricted to these explicit targets, it cannot be applied to assemblies, modules, parameters, return values, or generic parameters.

Compiler Resolution Behavior

When the compiler resolves a symbol marked with [Obsolete], it checks the context of the caller. If the calling member is also marked as [Obsolete], the compiler suppresses the warning or error. This cascading behavior allows deprecated members to safely reference other deprecated members internally without generating redundant compiler noise.
Master C# with Deep Grasping Methodology!Learn More