Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ f1_keywords:
- "CS8703" # ERR_InvalidModifierForLanguageVersion The modifier '{0}' is not valid for this item in C# {1}. Please use language version '{2}' or greater.
- "CS8704" # ERR_ImplicitImplementationOfNonPublicInterfaceMember Type does not implement interface member. Type cannot implicitly implement a non-public member in selected version.
- "CS8706" # ERR_LanguageVersionDoesNotSupportInterfaceImplementationForMember Type cannot implement interface member '{1}' in type '{2}' because feature '{3}' is not available in
- "CS8919"
- "CS8929"
- "CS8957" # ERR_NoImplicitConvTargetTypedConditional Conditional expression is not valid in language version {0} because a common type was not found between '{1}' and '{2}'
- "CS8912" # ERR_InheritingFromRecordWithSealedToString Inheriting from a record with a sealed 'Object.ToString' is not supported
- "CS9014" # ERR_UseDefViolationPropertyUnsupportedVersion Use of possibly unassigned property
Expand Down Expand Up @@ -87,6 +89,8 @@ helpviewer_keywords:
- "CS8706"
- "CS8957"
- "CS8912"
- "CS8919"
- "CS8929"
- "CS9014"
- "CS9015"
- "CS9016"
Expand Down Expand Up @@ -128,6 +132,8 @@ That's be design. The text closely matches the text of the compiler error / warn
- **CS8706**: *Type cannot implement interface member because a feature is not available in this version.*
- **CS8904**: *Invalid variance: The type parameter must be valid.*
- **CS8912**: *Inheriting from a record with a sealed 'Object.ToString' is not supported.*
- **CS8919**: *Cannot implement specified interface member in type because the target runtime doesn't support static abstract members in interfaces*
- **CS8929**: *Method cannot implement interface member in type because the target runtime doesn't support static abstract members in interfaces.*
- **CS8957**: *Conditional expression is not valid in language version because a common type was not found between types.*
- **CS8967**: *Newlines inside a non-verbatim interpolated string are not supported in C#*
- **CS9014**: *Error: Use of possibly unassigned property. Upgrade to auto-default the property.*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: Static abstract interface members errors and warnings
description: These warnings and errors are generated when static abstract or virtual members are used incorrectly. Learn how to correct these errors.
f1_keywords:
- "CS8920"
- "CS8921"
- "CS8922"
- "CS8923"
- "CS8924"
- "CS8925"
- "CS8926"
- "CS8928"
- "CS9030"
- "CS8931"
- "CS8932"
helpviewer_keywords:
- "CS8920"
- "CS8921"
- "CS8922"
- "CS8923"
- "CS8924"
- "CS8925"
- "CS8926"
- "CS8928"
- "CS8930"
- "CS8931"
- "CS8932"
ms.date: 11/29/2023
---
# Static abstract and virtual interface member errors and warnings

The compiler generates the following errors for invalid declarations of static abstract or virtual members in interfaces:

<!-- The text in this list generates issues for Acrolinx, because they don't use contractions.
That's be design. The text closely matches the text of the compiler error / warning for SEO purposes.
-->
- [**CS8920**](#errors-calling-static-abstract-interface-members): *The interface cannot be used as type argument. Static member does not have a most specific implementation in the interface.*
- [**CS8921**](#errors-in-interface-declaration): *The parameter of a unary operator must be the containing type, or its type parameter constrained to it.*
- [**CS8922**](#errors-in-interface-declaration): *The parameter type for `++` or `--` operator must be the containing type, or its type parameter constrained to it.*
- [**CS8923**](#errors-in-interface-declaration): *The return type for `++` or `--` operator must either match the parameter type, or be derived from the parameter type, or be the containing type's type parameter constrained to it unless the parameter type is a different type parameter.*
- [**CS8924**](#errors-in-interface-declaration): *One of the parameters of a binary operator must be the containing type, or its type parameter constrained to it.*
- [**CS8925**](#errors-in-interface-declaration): *The first operand of an overloaded shift operator must have the same type as the containing type or its type parameter constrained to it*
- [**CS8926**](#errors-calling-static-abstract-interface-members): *A static virtual or abstract interface member can be accessed only on a type parameter.*
- [**CS8928**](#errors-in-type-implementing-interface-declaration): *Type does not implement static interface member. The method cannot implement the interface member because it is not static.*
- [**CS8930**](#errors-in-type-implementing-interface-declaration): *Explicit implementation of a user-defined operator must be declared static*
- [**CS8931**](#errors-in-interface-declaration): *User-defined conversion in an interface must convert to or from a type parameter on the enclosing type constrained to the enclosing type*
- [**CS8932**](#errors-in-type-implementing-interface-declaration): *'UnmanagedCallersOnly' method cannot implement interface member in type*

These errors occur in three places in your code:

- When you [declare an interface with static abstract or virtual members](#errors-in-interface-declaration),
- When you [declare a type that implements an interface with static abstract or virtual members](#errors-in-type-implementing-interface-declaration), and
- When you access a [static abstract or virtual method declared in an interface](#errors-calling-static-abstract-interface-members).

## Errors in interface declaration

The following errors might occur when you declare an interface with `static abstact` or `static virtual` members:

- **CS8921**: *The parameter of a unary operator must be the containing type, or its type parameter constrained to it.*
- **CS8922**: *The parameter type for `++` or `--` operator must be the containing type, or its type parameter constrained to it.*
- **CS8923**: *The return type for `++` or `--` operator must either match the parameter type, or be derived from the parameter type, or be the containing type's type parameter constrained to it unless the parameter type is a different type parameter.*
- **CS8924**: *One of the parameters of a binary operator must be the containing type, or its type parameter constrained to it.*
- **CS8925**: *The first operand of an overloaded shift operator must have the same type as the containing type or its type parameter constrained to it*
- **CS8931**: *User-defined conversion in an interface must convert to or from a type parameter on the enclosing type constrained to the enclosing type*

All these rules are extensions of the rules for declaring overloaded operators. The distinction is that the parameter can be either the interface type, or the interface's type parameter if that type parameter is constrained to implement the interface for its type. For binary operators, only one parameter must satisfy this rule.

For example, `Inumber<T>` can declare an `T operator++(T)` because `T` is constrained to implement `INumber<T>`.

To fix these errors, ensure that the parameters of any operators defined in the interface obey these rules. You can learn more in the language reference article on [static abstract members in interfaces](../keywords/interface.md#static-abstract-and-virtual-members) or in the tutorial to [explore static abstract interface members](../../whats-new/tutorials/static-virtual-interface-members.md).

## Errors in type implementing interface declaration

The following errors might occur when you define a type that implements an interface with `static abstract` or `static virtual` methods:

- **CS8928**: *Type does not implement static interface member. The method cannot implement the interface member because it is not static.*
- **CS8930**: *Explicit implementation of a user-defined operator must be declared static*
- **CS8932**: *'UnmanagedCallersOnly' method cannot implement interface member in type*

These errors all indicate that you declared the method that implements a static abstract interface member incorrectly. These members must be declared `static`; they can't be instance members. Methods that implement interface members can't have the <xref:System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute?displayProperty=nameWithType> attribute applied to them.

## Errors calling static abstract interface members

The following errors might occur when you attempt to call a member defined as a static abstract or static virtual member of an interface:

- **CS8920**: *The interface cannot be used as type argument. Static member does not have a most specific implementation in the interface.*
- **CS8926**: *A static virtual or abstract interface member can be accessed only on a type parameter.*

Calls to interface members declared as `static abstract` or `static virtual` must be resolved to at compile-time. They must resolve to a static member defined in a type that implements that interface. That means you must access those members using either a concrete type that implements the interface, or a type parameter that is constrained to implement the interface. To fix these errors, change the type used to access the static member.
139 changes: 71 additions & 68 deletions docs/csharp/language-reference/compiler-messages/toc.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,77 @@
items:
- name: C# compiler messages
href: index.md
- name: Feature or version missing
href: feature-version-errors.md
displayName: >
CS0171, CS0188, CS0843, CS8904, CS1738, CS8022, CS8023, CS8024, CS8025, CS8026, CS8058, CS8059, CS8107, CS8192, CS8302,
CS8303, CS8304, CS8305, CS8306, CS8314, CS8320, CS8370, CS8371, CS8400, CS8401, CS8511, CS8627, CS8630, CS8652, CS8703,
CS8704, CS8706, CS8773, CS8912, CS8919, CS8929, CS8936, CS8957, CS8967, CS9014, CS9015, CS9016, CS9017, CS9058, CS9064,
CS9103, CS9171, CS9194, CS9202, CS9204
- name: Constructor declarations
href: constructor-errors.md
displayName: >
Primary constructors,
CS0514, CS0515, CS0516, CS0517, CS0522, CS0526, CS0568, CS0710, CS0768, CS0824, CS8054, CS8091, CS8358, CS8862, CS8867,
CS8868, CS8878, CS8910, CS8958, CS8982, CS8983, CS9105, CS9106, CS9107, CS9108, CS9109, CS9110, CS9111, CS9112, CS9113,
CS9114, CS9115, CS9116, CS9117, CS9118, CS9119, CS9120, CS9121, CS9122, CS9124, CS9136, CS9179
- name: Reference parameters
href: ref-modifiers-errors.md
displayName: >
ref safety,
CS0192, CS0199, CS0206, CS0631, CS0767, CS1510, CS1605, CS1623, CS1649, CS1651, CS1655, CS1657, CS1741, CS1939, CS1988,
CS7084, CS8166, CS8167, CS8168, CS8169. CS8325, CS8326, CS8327, CS8329, CS8330, CS8331, CS8332, CS8337, CS8338, CS8345,
CS8351, CS8373, CS8374, CS8388, CS8977, CS9072, CS9077, CS9078, CS9079, CS9085, CS9086, CS9087, CS9089, CS9091, CS9092,
CS9093, CS9094, CS9095, CS9096, CS9097, CS9101, CS9102, CS9104, CS9190, CS9191, CS9192, CS9193, CS9195, CS9196, CS9197,
CS9198, CS9199, CS9200, CS9201
- name: Nullable warnings
href: nullable-warnings.md
displayName: >
CS8597, CS8600, CS8601, CS8602, CS8604, CS8605, CS8607, CS8608, CS8609, CS8610, CS8611, CS8612, CS8613, CS8614, CS8615,
CS8616, CS8617, CS8618, CS8619, CS8620, CS8621, CS8622, CS8624, CS8625, CS8629, CS8631, CS8634, CS8655, CS8633, CS8643,
CS8644, CS8645, CS8762, CS8763, CS8764, CS8765, CS8766, CS8667, CS8768, CS8670, CS8714, CS8767, CS8769, CS8770, CS8774,
CS8776, CS8775, CS8777, CS8819, CS8824, CS8825, CS8847
- name: Pattern matching warnings
href: pattern-matching-warnings.md
displayName: CS8509, CS9134, CS9135
- name: Array declarations
href: array-declaration-errors.md
displayName: >
CS0022, CS0178, CS0248, CS0251, CS0270, CS0611, CS0623, CS0650, CS0719, CS0747, CS0820, CS0826, CS0846, CS1552, CS1586,
CS1920, CS1921, CS1925, CS1954, CS3007, CS3016, CS9174, CS9176, CS9185, CS9186, CS9187, CS9188, CS9203, CS9208, CS9209,
CS9210
- name: Inline arrays
href: inline-array-errors.md
displayName: CS9164, CS9165, CS9166, CS9167, CS9168, CS9169, CS9172, CS9173, CS9180, CS9181, CS9182, CS9183, CS9184
- name: Lambda expressions
href: lambda-expression-errors.md
displayName: >
CS0748, CS1621, CS1628, CS1632, CS1673, CS1686, CS1706, CS1989, CS8030, CS8175, CS8916, CS8971, CS8972, CS8975, CS9098,
CS9099, CS9100
- name: Restrictions on expression trees
href: expression-tree-restrictions.md
displayName: >
CS0765, CS0831, CS0832, CS0834, CS0835, CS0838, CS0845, CS0853, CS0854, CS0855, CS1944, CS1945, CS1946, CS1951, CS1952,
CS1963, CS2037, CS7053, CS8072, CS8074, CS8075, CS8110, CS8122, CS8143, CS8144, CS8153, CS8155, CS8198, CS8207, CS8382,
CS8514, CS8640, CS8642, CS8790, CS8791, CS8792, CS8810, CS8849, CS8927, CS8952, CS9170, CS9175
- name: Using directive and aliases
href: using-directive-errors.md
displayName: >
CS0105, CS0138, CS0431, CS0432, CS0440, CS0576, CS0687, CS1529, CS1537, CS7000, CS7007, CS8019, CS8083, CS8085, CS8914,
CS8915, CS8933, CS9055, CS9130, CS9131, CS9132, CS9133, CS9162, CS9163
- name: Source generators
href: source-generator-errors.md
displayName: >
CS9137, CS9138, CS9139, CS9140, CS9141, CS9142, CS9143, CS9144, CS9145, CS9146, CS9147, CS9148, CS9149, CS9150, CS9151,
CS9152, CS9153, CS9154, CS9155, CS9156, CS9157, CS9158, CS9159, CS9160, CS9161, CS9177, CS9178, CS9206, CS9207
- name: static abstract interface members
href: static-abstract-interfaces.md
displayName: CS8920, CS8921, CS8922, CS8923, CS8924, CS8925, CS8926, CS8928, CS8930, 8931, 8932
- name: Warning waves
href: warning-waves.md
displayName: >
CS7023, CS8073, CS8848, CS8880, CS8881, CS8882, CS8883, CS8884, CS8885, CS8886, CS8887, CS8892, CS8897, CS8898, CS8826,
CS8981
- name: Error messages
items:
- name: CS0001
Expand Down Expand Up @@ -1867,71 +1938,3 @@ items:
href: cs1610.md
- name: CS1712
href: ../../misc/cs1712.md
- name: Feature or version missing
href: feature-version-errors.md
displayName: >
CS0171, CS0188, CS0843, CS8904, CS1738, CS8022, CS8023, CS8024, CS8025, CS8026, CS8058, CS8059, CS8107, CS8192, CS8302,
CS8303, CS8304, CS8305, CS8306, CS8314, CS8320, CS8370, CS8371, CS8400, CS8401, CS8511, CS8627, CS8630, CS8652, CS8703,
CS8704, CS8706, CS8773, CS8912, CS8936, CS8957, CS8967, CS9014, CS9015, CS9016, CS9017, CS9058, CS9064, CS9103, CS9171,
CS9194, CS9202, CS9204
- name: Constructor declarations
href: constructor-errors.md
displayName: >
Primary constructors,
CS0514, CS0515, CS0516, CS0517, CS0522, CS0526, CS0568, CS0710, CS0768, CS0824, CS8054, CS8091, CS8358, CS8862, CS8867,
CS8868, CS8878, CS8910, CS8958, CS8982, CS8983, CS9105, CS9106, CS9107, CS9108, CS9109, CS9110, CS9111, CS9112, CS9113,
CS9114, CS9115, CS9116, CS9117, CS9118, CS9119, CS9120, CS9121, CS9122, CS9124, CS9136, CS9179
- name: Reference parameters
href: ref-modifiers-errors.md
displayName: >
ref safety,
CS0192, CS0199, CS0206, CS0631, CS0767, CS1510, CS1605, CS1623, CS1649, CS1651, CS1655, CS1657, CS1741, CS1939, CS1988,
CS7084, CS8166, CS8167, CS8168, CS8169. CS8325, CS8326, CS8327, CS8329, CS8330, CS8331, CS8332, CS8337, CS8338, CS8345,
CS8351, CS8373, CS8374, CS8388, CS8977, CS9072, CS9077, CS9078, CS9079, CS9085, CS9086, CS9087, CS9089, CS9091, CS9092,
CS9093, CS9094, CS9095, CS9096, CS9097, CS9101, CS9102, CS9104, CS9190, CS9191, CS9192, CS9193, CS9195, CS9196, CS9197,
CS9198, CS9199, CS9200, CS9201
- name: Nullable warnings
href: nullable-warnings.md
displayName: >
CS8597, CS8600, CS8601, CS8602, CS8604, CS8605, CS8607, CS8608, CS8609, CS8610, CS8611, CS8612, CS8613, CS8614, CS8615,
CS8616, CS8617, CS8618, CS8619, CS8620, CS8621, CS8622, CS8624, CS8625, CS8629, CS8631, CS8634, CS8655, CS8633, CS8643,
CS8644, CS8645, CS8762, CS8763, CS8764, CS8765, CS8766, CS8667, CS8768, CS8670, CS8714, CS8767, CS8769, CS8770, CS8774,
CS8776, CS8775, CS8777, CS8819, CS8824, CS8825, CS8847
- name: Pattern matching warnings
href: pattern-matching-warnings.md
displayName: CS8509, CS9134, CS9135
- name: Array declarations
href: array-declaration-errors.md
displayName: >
CS0022, CS0178, CS0248, CS0251, CS0270, CS0611, CS0623, CS0650, CS0719, CS0747, CS0820, CS0826, CS0846, CS1552, CS1586,
CS1920, CS1921, CS1925, CS1954, CS3007, CS3016, CS9174, CS9176, CS9185, CS9186, CS9187, CS9188, CS9203, CS9208, CS9209,
CS9210
- name: Inline arrays
href: inline-array-errors.md
displayName: CS9164, CS9165, CS9166, CS9167, CS9168, CS9169, CS9172, CS9173, CS9180, CS9181, CS9182, CS9183, CS9184
- name: Lambda expressions
href: lambda-expression-errors.md
displayName: >
CS0748, CS1621, CS1628, CS1632, CS1673, CS1686, CS1706, CS1989, CS8030, CS8175, CS8916, CS8971, CS8972, CS8975, CS9098,
CS9099, CS9100
- name: Restrictions on expression trees
href: expression-tree-restrictions.md
displayName: >
CS0765, CS0831, CS0832, CS0834, CS0835, CS0838, CS0845, CS0853, CS0854, CS0855, CS1944, CS1945, CS1946, CS1951, CS1952,
CS1963, CS2037, CS7053, CS8072, CS8074, CS8075, CS8110, CS8122, CS8143, CS8144, CS8153, CS8155, CS8198, CS8207, CS8382,
CS8514, CS8640, CS8642, CS8790, CS8791, CS8792, CS8810, CS8849, CS8927, CS8952, CS9170, CS9175
- name: Using directive and aliases
href: using-directive-errors.md
displayName: >
CS0105, CS0138, CS0431, CS0432, CS0440, CS0576, CS0687, CS1529, CS1537, CS7000, CS7007, CS8019, CS8083, CS8085, CS8914,
CS8915, CS8933, CS9055, CS9130, CS9131, CS9132, CS9133, CS9162, CS9163
- name: Source generators
href: source-generator-errors.md
displayName: >
CS9137, CS9138, CS9139, CS9140, CS9141, CS9142, CS9143, CS9144, CS9145, CS9146, CS9147, CS9148, CS9149, CS9150, CS9151,
CS9152, CS9153, CS9154, CS9155, CS9156, CS9157, CS9158, CS9159, CS9160, CS9161, CS9177, CS9178, CS9206, CS9207
- name: Warning waves
href: warning-waves.md
displayName: >
CS7023, CS8073, CS8848, CS8880, CS8881, CS8882, CS8883, CS8884, CS8885, CS8886, CS8887, CS8892, CS8897, CS8898, CS8826,
CS8981
Original file line number Diff line number Diff line change
Expand Up @@ -525,19 +525,6 @@ f1_keywords:
- "CS8913"
- "CS8917"
- "CS8918"
- "CS8919"
- "CS8920"
- "CS8921"
- "CS8922"
- "CS8923"
- "CS8924"
- "CS8925"
- "CS8926"
- "CS8928"
- "CS8929"
- "CS8930"
- "CS8931"
- "CS8932"
- "CS8934"
- "CS8935"
- "CS8937"
Expand Down