Skip to content

Commit a4cbd47

Browse files
committed
Add additional attributes
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.
1 parent d9e64e6 commit a4cbd47

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

docs/csharp/language-reference/attributes/general.md

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
---
22
title: "Attributes interpreted by the compiler: Miscellaneous"
3-
ms.date: 11/22/2024
3+
ms.date: 01/24/2025
44
description: "Learn about attributes that affect code generated by the compiler: the Conditional, Obsolete, AttributeUsage, ModuleInitializer, and SkipLocalsInit attributes."
55
---
66
# Miscellaneous attributes interpreted by the C# compiler
77

88
There are several attributes that can be applied to elements in your code that add semantic meaning to those elements:
99

1010
- [`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.
1215
- [`AttributeUsage`](#attributeusage-attribute): Declare the language elements where an attribute can be applied.
1316
- [`AsyncMethodBuilder`](#asyncmethodbuilder-attribute): Declare an async method builder type.
1417
- [`InterpolatedStringHandler`](#interpolatedstringhandler-and-interpolatedstringhandlerarguments-attributes): Define an interpolated string builder for a known scenario.
1518
- [`ModuleInitializer`](#moduleinitializer-attribute): Declare a method that initializes a module.
1619
- [`SkipLocalsInit`](#skiplocalsinit-attribute): Elide the code that initializes local variable storage to 0.
1720
- [`UnscopedRef`](#unscopedref-attribute): Declare that a `ref` variable normally interpreted as `scoped` should be treated as unscoped.
1821
- [`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.
2027

2128
The compiler uses those semantic meanings to alter its output and report possible mistakes by developers using your code.
2229

@@ -46,7 +53,7 @@ The `Conditional` attribute can also be applied to an attribute class definition
4653

4754
:::code language="csharp" source="snippets/ConditionalExamples.cs" ID="SnippetConditionalConditionalAttribute" :::
4855

49-
## `Obsolete` attribute
56+
## `Obsolete` and `Deprecated` attribute
5057

5158
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>.
5259

@@ -58,7 +65,9 @@ The string provided as the first argument to the attribute constructor is displa
5865

5966
:::code language="csharp" source="snippets/ObsoleteExample.cs" id="Snippet2" :::
6067

61-
## `Experimental` attribute
68+
The Windows Foundation Metadata libraries use the <xref:Windows.Foundation.Metadata.DeprecatedAttribute?displayProperty=nameWithType> instead of the `ObsoleteAttribute`.
69+
70+
## `Experimental` attributes
6271

6372
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.
6473

@@ -67,6 +76,8 @@ Beginning in C# 12, types, methods, and assemblies can be marked with the <xref:
6776
6877
You can read more details about the `Experimental` attribute in the [feature specification](~/_csharplang/proposals/csharp-12.0/experimental-attribute.md).
6978

79+
The Windows Foundation Metadata libraries use the <xref:Windows.Foundation.Metadata.ExperimentalAttribute?displayProperty=nameWithType>, which predates C# 12.
80+
7081
## `SetsRequiredMembers` attribute
7182

7283
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
242253

243254
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.
244255

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)`.
271+
245272
## See also
246273

247274
- <xref:System.Attribute>

docs/csharp/language-reference/attributes/pseudo-attributes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ You add these attributes to your code for the compiler to emit specified Interme
1818
- <xref:System.Runtime.InteropServices.PreserveSigAttribute?displayProperty=fullName>: Specifies the IL `preservesig` modifier.
1919
- <xref:System.SerializableAttribute?displayProperty=fullName>: Specifies the IL `serializable` modifier.
2020
- <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.
2122

2223
Other custom attributes are generally applied using other C# syntax rather than adding the attribute to your source code.
2324

2425
- <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)
2526
- <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).
2627
- <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.

docs/csharp/language-reference/toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ items:
395395
displayName: >
396396
ComImportAttribute, DefaultParameterValueAttribute, DllImportAttribute, FieldOffsetAttribute, InAttribute,
397397
MarshalAsAttribute, MethodImplAttribute, NonSerializedAttribute, OptionalAttribute, OutAttribute, PreserveSigAttribute,
398-
SerializableAttribute, SkipLocalsInitAttribute, SpecialNameAttribute, StructLayoutAttribute
398+
SerializableAttribute, SkipLocalsInitAttribute, SpecialNameAttribute, StructLayoutAttribute, IndexerNameAttribute
399399
href: ./attributes/pseudo-attributes.md
400400
- name: Unsafe code and pointers
401401
displayName: unsafe code, pointers, fixed size buffer, function pointer

0 commit comments

Comments
 (0)