Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Reflection, Attributes and Metadata

Built-in C# attributes

Reference for common built-in C# attributes: Obsolete, Conditional, Flags, CallerMemberName, InlineArray, SkipLocalsInit and more, with runnable examples.

The base class library ships dozens of attributes that the compiler or runtime treats specially. They fall into a few groups: diagnostics (Obsolete, Conditional, Experimental), caller information (CallerMemberName, CallerFilePath, CallerLineNumber, CallerArgumentExpression), language features (Flags, InlineArray, ModuleInitializer, SkipLocalsInit, InterpolatedStringHandler), and optimisation or interop hints (MethodImpl, StructLayout, DllImport).

This page covers the ones you will meet most. Nullable-analysis attributes have their own page.

C#
[Obsolete("Use Run instead", error: false)]
[Flags] enum Perm { None = 0, Read = 1, Write = 2 }
void Log(string msg, [CallerMemberName] string caller = "") { }

Try it

C# Obsolete, Flags and caller information

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Conditional, ModuleInitializer and MethodImpl

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

Attribute Applies to Effect
[Obsolete("msg", error)] Almost anything Warning CS0618 (or error CS0619 when error: true) at every use. Use DiagnosticId to give it a stable code.
[Conditional("SYMBOL")] void methods, attribute classes Calls are removed by the compiler when SYMBOL is not defined. Arguments are not evaluated either.
[Flags] enum Declares a bit set: ToString and Parse combine names. Values should be powers of two.
[CallerMemberName], [CallerFilePath], [CallerLineNumber] Optional parameters Compiler fills the argument from the call site.
[CallerArgumentExpression("p")] Optional string parameter Receives the source text of argument p.
[ModuleInitializer] Static void method with no parameters Runs once when the module loads, before other code in it.
[MethodImpl(options)] Methods Hints such as AggressiveInlining and NoInlining.
[SkipLocalsInit] Methods, types, modules Skips zeroing of stackalloc and locals; needs unsafe enabled.
[InlineArray(n)] struct with one field Makes the struct a fixed-size inline array (C# 12).
[Experimental("ID")] Types, members Warns that an API may change; consumers opt in by suppressing the diagnostic.
[DoesNotReturn] Methods See nullable analysis attributes.

Usage notes

  • [Obsolete] is a warning by default. Set error: true once callers have had time to migrate, and always say what to use instead.
  • [Conditional] removes the whole call. Argument expressions with side effects are skipped too, so do not rely on them. Debug.WriteLine and Debug.Assert use this; the runner builds in Release mode, so they vanish unless DEBUG is defined.
  • #define must come first in the file, before any code or using. Defining the symbol in the caller's file is what matters for [Conditional].
  • Caller attributes only work on optional parameters, and an explicitly passed argument wins over the compiler-supplied one.
  • [Flags] does not validate anything. (Perm)8 is legal and prints 8; it also does not assign the powers of two for you.
  • [MethodImpl(AggressiveInlining)] is a hint to the JIT; measure before adding it everywhere.
  • [ModuleInitializer] is meant for libraries; do not use it for ordinary application start-up, where an explicit Main is clearer.
  • Custom attributes with [AttributeUsage] are covered under Custom attributes.