Skip to content

Commit 03e3249

Browse files
authored
support required members
1 parent 40f3671 commit 03e3249

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

standard/attributes.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ A number of attributes affect the language in some way. These attributes include
498498
- `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.
499499
- `System.Runtime.CompilerServices.ModuleInitializer` ([§23.5.9](attributes.md#2359-the-moduleinitializer-attribute)), which is used to mark a method as a module initializer.
500500
- `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.
501+
- `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)).
501502
502503
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)).
503504
@@ -1428,6 +1429,65 @@ Attribute `InterpolatedStringHandlerArgument` is applied to the handler paramete
14281429

14291430
If an `out bool` parameter is also declared to allow the handler to be inhibited ([§23.5.9.1.2](attributes.md#235912-inhibiting-a-custom-handler)) that parameter shall be the final one.
14301431

1432+
### §Required-Member-Attributes Required member attributes
1433+
1434+
#### §SetsRequiredMembers The SetsRequiredMembers attribute
1435+
1436+
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.
1437+
1438+
> *Example*:
1439+
>
1440+
> <!-- Example: {template:"standalone-lib", name:"SetsRequiredMembers", expectedErrors:["CS9035","CS9035"]} -->
1441+
> ```csharp
1442+
> public class Person
1443+
> {
1444+
> public Person() { }
1445+
>
1446+
> [SetsRequiredMembers]
1447+
> public Person(string firstName, string lastName) =>
1448+
> (FirstName, LastName) = (firstName, lastName);
1449+
>
1450+
> public required string FirstName { get; init; }
1451+
> public required string LastName { get; init; }
1452+
>
1453+
> public int? Age { get; set; }
1454+
> }
1455+
>
1456+
> public class Student : Person
1457+
> {
1458+
> public Student() : base()
1459+
> {
1460+
> }
1461+
>
1462+
> [SetsRequiredMembers]
1463+
> public Student(string firstName, string lastName) :
1464+
> base(firstName, lastName)
1465+
> {
1466+
> }
1467+
>
1468+
> public double GPA { get; set; }
1469+
> }
1470+
>
1471+
> public class Test
1472+
> {
1473+
> public static void M()
1474+
> {
1475+
> var p1 = new Student(); // error: doesn't set required members
1476+
> var p2 = new Student("Jane", "Williams"); // OK
1477+
> }
1478+
> }
1479+
> ```
1480+
>
1481+
> *end example*
1482+
<!-- markdownlint-disable MD028 -->
1483+
1484+
<!-- markdownlint-enable MD028 -->
1485+
> *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*
1486+
1487+
#### §RequiredMember The RequiredMember attribute
1488+
1489+
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.
1490+
14311491
## 23.6 Attributes for interoperation
14321492
14331493
For interoperation with other languages, an indexer may be implemented using indexed properties. If no `IndexerName` attribute is present for an indexer, then the name `Item` is used by default. The `IndexerName` attribute enables a developer to override this default and specify a different name.

0 commit comments

Comments
 (0)