You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fixes#43109
Most of these have been added to the general page. A few are expressed in source differently, and were added to the new pseudo-attributes page.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/attributes/general.md
+32-5Lines changed: 32 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,29 @@
1
1
---
2
2
title: "Attributes interpreted by the compiler: Miscellaneous"
3
-
ms.date: 11/22/2024
3
+
ms.date: 01/24/2025
4
4
description: "Learn about attributes that affect code generated by the compiler: the Conditional, Obsolete, AttributeUsage, ModuleInitializer, and SkipLocalsInit attributes."
5
5
---
6
6
# Miscellaneous attributes interpreted by the C# compiler
7
7
8
8
There are several attributes that can be applied to elements in your code that add semantic meaning to those elements:
9
9
10
10
-[`Conditional`](#conditional-attribute): Make execution of a method dependent on a preprocessor identifier.
11
-
-[`Obsolete`](#obsolete-attribute): Mark a type or member for (potential) future removal.
11
+
-[`Obsolete`](#obsolete-and-deprecated-attribute): Mark a type or member for (potential) future removal.
12
+
-[`Deprecated`](#obsolete-and-deprecated-attribute): (Windows Foundation) Mark a type or member for (potential) future removal.
13
+
-[`Experimental`](#experimental-attribute): Mark a type or member as experimental.
14
+
-[`SetsRequiredMembers`](#setsrequiredmembers-attribute): Indicate that a constructor sets all required properties.
12
15
-[`AttributeUsage`](#attributeusage-attribute): Declare the language elements where an attribute can be applied.
13
16
-[`AsyncMethodBuilder`](#asyncmethodbuilder-attribute): Declare an async method builder type.
14
17
-[`InterpolatedStringHandler`](#interpolatedstringhandler-and-interpolatedstringhandlerarguments-attributes): Define an interpolated string builder for a known scenario.
15
18
-[`ModuleInitializer`](#moduleinitializer-attribute): Declare a method that initializes a module.
16
19
-[`SkipLocalsInit`](#skiplocalsinit-attribute): Elide the code that initializes local variable storage to 0.
17
20
-[`UnscopedRef`](#unscopedref-attribute): Declare that a `ref` variable normally interpreted as `scoped` should be treated as unscoped.
18
21
-[`OverloadResolutionPriority`](#overloadresolutionpriority-attribute): Add a tiebreaker attribute to influence overload resolution for possibly ambiguous overloads.
19
-
-[`Experimental`](#experimental-attribute): Mark a type or member as experimental.
22
+
-[`EnumeratorCancellation`](#enumeratorcancellation-attribute): Specify the parameter for a cancellation token in an async enumerator.
23
+
-[`CollectionBuilder`](#collectionbuilder-attribute): Specify the builder method for a collection type when a collection expression is converted to that collection type.
24
+
-[`InlineArray`](#inlinearray-attribute): Specify that a `struct` type is an *inline array*.
25
+
-[`IUnknownConstant`](#iunknownconstant-and-idispatchconstant-attributes): Specifies how a missing argument should be supplied for a default parameter.
26
+
-[`IDispatchConstant`](#iunknownconstant-and-idispatchconstant-attributes): Specifies how a missing argument should be supplied for a default parameter.
20
27
21
28
The compiler uses those semantic meanings to alter its output and report possible mistakes by developers using your code.
22
29
@@ -46,7 +53,7 @@ The `Conditional` attribute can also be applied to an attribute class definition
The `Obsolete` attribute marks a code element as no longer recommended for use. Use of an entity marked obsolete generates a warning or an error. The `Obsolete` attribute is a single-use attribute and can be applied to any entity that allows attributes. `Obsolete` is an alias for <xref:System.ObsoleteAttribute>.
52
59
@@ -58,7 +65,9 @@ The string provided as the first argument to the attribute constructor is displa
The Windows Foundation Metadata libraries use the <xref:Windows.Foundation.Metadata.DeprecatedAttribute?displayProperty=nameWithType> instead of the `ObsoleteAttribute`.
69
+
70
+
## `Experimental` attributes
62
71
63
72
Beginning in C# 12, types, methods, and assemblies can be marked with the <xref:System.Diagnostics.CodeAnalysis.ExperimentalAttribute?displayProperty=nameWithType> to indicate an experimental feature. The compiler issues a warning if you access a method or type annotated with the <xref:System.Diagnostics.CodeAnalysis.ExperimentalAttribute>. All types declared in an assembly or module marked with the `Experimental` attribute are experimental. The compiler issues a warning if you access any of them. You can disable these warnings to pilot an experimental feature.
64
73
@@ -67,6 +76,8 @@ Beginning in C# 12, types, methods, and assemblies can be marked with the <xref:
67
76
68
77
You can read more details about the `Experimental` attribute in the [feature specification](~/_csharplang/proposals/csharp-12.0/experimental-attribute.md).
69
78
79
+
The Windows Foundation Metadata libraries use the <xref:Windows.Foundation.Metadata.ExperimentalAttribute?displayProperty=nameWithType>, which predates C# 12.
80
+
70
81
## `SetsRequiredMembers` attribute
71
82
72
83
The `SetsRequiredMembers` attribute informs the compiler that a constructor sets all `required` members in that class or struct. The compiler assumes any constructor with the <xref:System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute?displayProperty=fullName> attribute initializes all `required` members. Any code that invokes such a constructor doesn't need object initializers to set required members. Adding the `SetsRequiredMembers` attribute is primarily useful for positional records and primary constructors.
@@ -242,6 +253,22 @@ Overload resolution considers the two methods equally good for some argument typ
242
253
243
254
All overloads with a lower priority than the highest overload priority are removed from the set of applicable methods. Methods without this attribute have the overload priority set to the default of zero. Library authors should use this attribute as a last resort when adding a new and better method overload. Library authors should have a deep understanding of how [Overload resolution](~/_csharplang/proposals/csharp-13.0/overload-resolution-priority.md#overload-resolution-priority) impacts choosing the better method. Otherwise, unexpected errors can result.
244
255
256
+
## EnumeratorCancellation attribute
257
+
258
+
This attributes specifies which parameter should receive the cancellation token from <xref:System.Collections.Generic.IAsyncEnumerable%2A1.GetAsyncEnumerator(System.Threading.CancellationToken)?displayProperty=nameWithType> API. It's part of the infrastructure for the [async streams](../../asynchronous-programming/generate-consume-asynchronous-stream.md) feature.
259
+
260
+
## CollectionBuilder attribute
261
+
262
+
The <xref:System.Runtime.CompilerServices.CollectionBuilderAttribute?displayProperty=nameWithType> specifies a method that builds an instance of a collection type from a [collection expression](../operators/collection-expressions.md). You use this attribute to specify a method that builds the collection. The compiler will generate code to call that method when a collection expression is converted to that type.
263
+
264
+
## InlineArray attribute
265
+
266
+
The <xref:System.Runtime.CompilerServices.InlineArrayAttribute?displayProperty=nameWithType> attribute marks a type as an [inline array](../builtin-types/struct.md#inline-arrays). You can learn more about this feature in the article on `structs`, in the section on [inline arrays](../builtin-types/struct.md#inline-arrays).
267
+
268
+
## IUnknownConstant and IDispatchConstant attributes
269
+
270
+
The <xref:System.Runtime.CompilerServices.IUnknownConstantAttribute?displayProperty=nameWithType> and <xref:System.Runtime.CompilerServices.IDispatchConstantAttribute?displayProperty=nameWithType> attributes can be added to default parameters to indicate that a missing argument should be supplied as `new UnknownWrapper(null)` or `new DispatchWrapper(null)`.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/attributes/pseudo-attributes.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,9 +18,11 @@ You add these attributes to your code for the compiler to emit specified Interme
18
18
-<xref:System.Runtime.InteropServices.PreserveSigAttribute?displayProperty=fullName>: Specifies the IL `preservesig` modifier.
19
19
-<xref:System.SerializableAttribute?displayProperty=fullName>: Specifies the IL `serializable` modifier.
20
20
-<xref:System.Runtime.InteropServices.StructLayoutAttribute?displayProperty=fullName>: Specifies the IL `auto`, `sequential`, or `explicit` modifiers. Layout options can be set using the parameters.
21
+
-<xref:System.Runtime.CompilerServices.IndexerNameAttribute?displayProperty=fullName>: You add this attribute to an indexer to set a different method name. By default, indexers are compiled to a property named `Item`. You can specify a different name using this attribute.
21
22
22
23
Other custom attributes are generally applied using other C# syntax rather than adding the attribute to your source code.
23
24
24
25
-<xref:System.Runtime.InteropServices.DefaultParameterValueAttribute?displayProperty=fullName>: Specifies the default value for the parameter. Use the [default parameter syntax](../../methods.md#optional-parameters-and-arguments)
25
26
-<xref:System.Runtime.InteropServices.InAttribute?displayProperty=fullName>: Specifies the IL `[in]` modifier. Use the [`in`](../keywords/method-parameters.md#in-parameter-modifier) or [`ref readonly`](../keywords/method-parameters.md#ref-readonly-modifier).
26
27
-<xref:System.Runtime.CompilerServices.SpecialNameAttribute?displayProperty=fullName>: Specifies the IL `specialname` modifier. The compiler automatically uses the for methods that require the `specialname` modifier.
28
+
-<xref:System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute?displayProperty=nameWithType>: This attribute is required for the `delegate*` feature. The compiler adds it to any [`delegate*`](../unsafe-code.md#function-pointers) requires its use.
0 commit comments