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
25 changes: 24 additions & 1 deletion standard/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class_modifier
| 'sealed'
| 'static'
| 'file'
| 'closed'
| unsafe_modifier // unsafe code support
;
```
Expand All @@ -83,7 +84,15 @@ It is a compile-time error for any of the `public`, `protected`, `internal`, and

When a partial type declaration ([§15.2.7](classes.md#1527-partial-type-declarations)) includes an accessibility specification (via the `public`, `protected`, `internal`, and `private` modifiers), that specification shall agree with all other parts that include an accessibility specification. If no part of a partial type includes an accessibility specification, the type is given the appropriate default accessibility ([§7.4.2](basic-concepts.md#742-declared-accessibility)).

The `abstract`, `sealed`, and `static` modifiers are discussed in the following subclauses.
The `abstract`, `sealed`, `static`, and `closed` modifiers are discussed in the following subclauses.

The `closed` modifier requires all classes directly derived from a ***closed class*** to be declared in the same assembly and module as that closed class. (`closed` is a contextual keyword ([§6.4.4](lexical-structure.md#644-keywords)) that has special meaning when used as a *class_modifier*.)

A closed class is implicitly abstract and may not be declared so explicitly. A closed class may not have a `sealed` or `static` modifier.

A class derived from a closed class is not itself closed unless explicitly declared to be.

If a generic class is directly derived from a closed class, all of that generic class’s type parameters shall be used in its *class_base*.

The `file` modifier specifies that the type being declared is local to its parent compilation unit. A compilation unit containing a `file`-modified type shall not also contain a type declaration having the same name but without the `file` modifier. (`file` is a contextual keyword ([§6.4.4](lexical-structure.md#644-keywords)) that has special meaning when used as a top-level type modifier.)

Expand Down Expand Up @@ -181,6 +190,20 @@ In any other context, it is a compile-time error to reference a static class.

> *Note*: For example, it is an error for a static class to be used as a base class, a constituent type ([§15.3.7](classes.md#1537-constituent-types)) of a member, a generic type argument, or a type parameter constraint. Likewise, a static class cannot be used in an array type, a new expression, a cast expression, an is expression, an as expression, a `sizeof` expression, or a default value expression. *end note*

#### §closed-classes Closed classes

Declaring a class to be closed indicates that a set of classes directly derived from that class is complete, allowing consuming code to rely on that for exhaustiveness in a *switch expression* ([§12.12](expressions.md#1212-switch-expression)). However, when a closed class has no subclasses, an empty switch over it is not considered exhaustive.

Exhaustiveness of a switch over a closed class type is determined by checking if the switch is exhaustive over the set of subclasses of the input closed class type. This set of subclasses `S` is determined in the following way:

1. For a given closed type `C`, let `C₀` be its original definition.
1. For each subclass declaration `S₀` whose base type has original definition `C₀`, determine if a construction `S` exists which has base type `C` (see [§19.6.3](interfaces.md#1963-uniqueness-of-implemented-interfaces)).
1. If such an `S` exists, it is included in the set of subclasses.

A closed class is said to have a ***sealed hierarchy***, if all its subclasses are either sealed or have a sealed hierarchy. That is, all the classes in the expanded hierarchy are either sealed or closed.

When a closed class has a sealed hierarchy, an interface convertibility restriction is introduced. This prevents attempting a conversion to interface type, which could never possibly succeed.

### 15.2.3 Type parameters

A type parameter is a simple identifier that denotes a placeholder for a type argument supplied to create a constructed type. By contrast, a type argument ([§8.4.2](types.md#842-type-arguments)) is the type that is substituted for the type parameter when a constructed type is created.
Expand Down
2 changes: 1 addition & 1 deletion standard/lexical-structure.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# 6 Lexical structure

## 6.1 Programs

Check warning on line 3 in standard/lexical-structure.md

View workflow job for this annotation

GitHub Actions / word-converter

Markdown to Word Converter - MDC032

Line length 89 > maximum 83; | 'async' | 'await' | 'by' | 'Cdecl' | 'closed' | 'descending'

A C# ***program*** consists of one or more source files, each known formally as a ***compilation unit*** ([§14.2](namespaces.md#142-compilation-units)). Although a compilation unit might have a one-to-one correspondence with a file in a file system, such correspondence is not required.

Conceptually speaking, a program is compiled using three steps:

1. Transformation, which converts a file from a particular character repertoire and encoding scheme into a sequence of Unicode characters.
1. Lexical analysis, which translates a stream of Unicode input characters into a stream of tokens.

Check warning on line 10 in standard/lexical-structure.md

View workflow job for this annotation

GitHub Actions / word-converter

Markdown to Word Converter - MDC032

Line length 85 > maximum 83; | PP_Start_Line_Character PP_Whitespace? '-' PP_Whitespace? PP_End_Line_Character
1. Syntactic analysis, which translates the stream of tokens into executable code.

Apart from accepting UTF-8 encoded input (as required by [§5](conformance.md#5-conformance), a conforming implementation may choose to accept and transform additional character encoding schemes (such as UTF-16, UTF-32, or non-Unicode character mappings).
Expand Down Expand Up @@ -624,7 +624,7 @@
```ANTLR
contextual_keyword
: 'add' | 'alias' | 'allows' | 'and' | 'ascending'
| 'async' | 'await' | 'by' | 'Cdecl' | 'descending'
| 'async' | 'await' | 'by' | 'Cdecl' | 'closed' | 'descending'
| 'dynamic' | 'equals' | 'extension' | 'field' | 'Fastcall'
| 'file' | 'from' | 'get' | 'global' | 'group'
| 'init' | 'into' | 'join' | 'let' | 'managed'
Expand Down
Loading