Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions standard/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ A number of attributes affect the language in some way. These attributes include
- `System.Runtime.CompilerServices.EnumeratorCancellationAttribute` ([§23.5.8](attributes.md#2358-the-enumeratorcancellation-attribute)), which is used to specify parameter for the cancellation token in an asynchronous iterator.
- `System.Runtime.CompilerServices.ModuleInitializerAttribute` ([§23.5.9](attributes.md#2359-the-moduleinitializer-attribute)), which is used to mark a method as a module initializer.
- `System.Runtime.CompilerServices.InterpolatedStringHandlerAttribute` and `System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute`, which are used to declare a custom interpolated string expression handler ([§23.5.9.1](attributes.md#23591-custom-interpolated-string-expression-handlers)) and to call one of its constructors, respectively.
- `System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute` (§SetsRequiredMembers) and `System.Runtime.CompilerServices.RequiredMemberAttribute` (§RequiredMember), which are used in required-member contexts ([§15.7.1](classes.md#1571-general)).

The Nullable static analysis attributes ([§23.5.7](attributes.md#2357-code-analysis-attributes)) can improve the correctness of warnings generated for nullabilities and null states ([§8.9.5](types.md#895-nullabilities-and-null-states)).

Expand Down Expand Up @@ -733,6 +734,8 @@ The attribute `ObsoleteAttribute` is used to mark types and members of types tha

If a program uses a type or member that is decorated with `ObsoleteAttribute`, a compiler shall issue a warning or an error. Specifically, a compiler shall issue a warning if no error parameter is provided, or if the error parameter is provided and has the value `false`. A compiler shall issue an error if the error parameter is specified and has the value `true`.

A compiler may issue a warning when a required member ([§15.7.1](classes.md#1571-general)) is decorated with `ObsoleteAttribute`, unless its containing type is also decorated with `ObsoleteAttribute` or every instance constructor of that type is decorated with either `ObsoleteAttribute` or `SetsRequiredMembersAttribute` (§SetsRequiredMembers).

> *Example*: In the following code
>
> <!-- Example: {template:"standalone-console", name:"ObsoleteAttribute", expectedWarnings:["CS0618","CS0618"]} -->
Expand Down Expand Up @@ -1634,6 +1637,65 @@ Constructor overload resolution uses the parameter types of the containing membe

> *Note*: In particular, a forwarded lambda is not independently bound to a different delegate type for the constructor. Handler constructor parameter types do not contribute to generic inference for the containing invocation. Nullable analysis of constructor arguments does not flow changes back to the original receiver or argument slots. *end note*

### §Required-Member-Attributes Required member attributes

#### §SetsRequiredMembers The SetsRequiredMembers attribute

This attribute indicates that the constructor it decorates sets all required members for the current type, so callers do not need to set any required members themselves. However, the compiler doesn't verify that the constructor actually initializes all required members.

> *Example*:
>
> <!-- Example: {template:"standalone-lib", name:"SetsRequiredMembers", expectedErrors:["CS9035","CS9035"]} -->
> ```csharp
> public class Person
> {
> public Person() { }
>
> [SetsRequiredMembers]
> public Person(string firstName, string lastName) =>
> (FirstName, LastName) = (firstName, lastName);
>
> public required string FirstName { get; init; }
> public required string LastName { get; init; }
>
> public int? Age { get; set; }
> }
>
> public class Student : Person
> {
> public Student() : base()
> {
> }
>
> [SetsRequiredMembers]
> public Student(string firstName, string lastName) :
> base(firstName, lastName)
> {
> }
>
> public double GPA { get; set; }
> }
>
> public class Test
> {
> public static void M()
> {
> var p1 = new Student(); // error: doesn't set required members
> var p2 = new Student("Jane", "Williams"); // OK
> }
> }
> ```
>
> *end example*
<!-- markdownlint-disable MD028 -->

<!-- markdownlint-enable MD028 -->
> *Note*: As the derived-type constructor `Student(string, string)` chains to the base-type constructor `Person(string, string)`, which has this attribute, the derived-type constructor must also have that attribute ([§15.11.1](classes.md#15111-general)). *end note*

#### §RequiredMember The RequiredMember attribute

This attribute indicates that the current type has one or more required members ([§15.7.1](classes.md#1571-general)), or that a specific member of that type is required. However, it is an error for this attribute to be used explicitly. Instead, the presence of the modifier `required` results in the type or member being treated as if it were decorated with this attribute.

## 23.6 Attributes for interoperation

For interoperation with other languages, an indexer may be implemented using indexed properties. If the indexer is not decorated with `IndexerNameAttribute`, then the name `Item` is used by default. `IndexerNameAttribute` enables a developer to override this default and specify a different name.
Expand Down
Loading
Loading