Skip to content

Commit 0530114

Browse files
CopilotBillWagner
andcommitted
Fix incorrect statement about generic interface inheritance
The previous statement incorrectly claimed that generic interfaces can only inherit from non-generic interfaces if they are covariant. This is false - any generic interface can inherit from a non-generic interface regardless of variance. The corrected text now: 1. States correctly that generic interfaces CAN inherit from non-generic interfaces (no conditions) 2. Explains that when this happens, the type parameter replaces 'object' in overridden members 3. Uses IEnumerable<T> as an example where T replaces object in output positions 4. Clarifies that because T is used only in output positions, the interface CAN be marked as covariant 5. Notes that if T were used in input positions, it couldn't be covariant This addresses the issue raised in #43518 and incorporates Bill Wagner's clarification. Co-authored-by: BillWagner <[email protected]>
1 parent d7e3e42 commit 0530114

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

docs/csharp/programming-guide/generics/generic-interfaces.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The rules of inheritance that apply to classes also apply to interfaces:
2626

2727
:::code language="csharp" source="./snippets/GenericInterfaces.cs" id="Months":::
2828

29-
Generic interfaces can inherit from non-generic interfaces if the generic interface is covariant, which means it only uses its type parameter as a return value. In the .NET class library, <xref:System.Collections.Generic.IEnumerable%601> inherits from <xref:System.Collections.IEnumerable> because <xref:System.Collections.Generic.IEnumerable%601> only uses `T` in the return value of <xref:System.Collections.Generic.IEnumerable%601.GetEnumerator%2A> and in the <xref:System.Collections.Generic.IEnumerator%601.Current%2A> property getter.
29+
Generic interfaces can inherit from non-generic interfaces. In the .NET class library, <xref:System.Collections.Generic.IEnumerable%601> inherits from <xref:System.Collections.IEnumerable>. When a generic interface inherits from a non-generic interface, the type parameter replaces `object` in the overridden members. For example, <xref:System.Collections.Generic.IEnumerable%601> uses `T` in place of `object` in the return value of <xref:System.Collections.Generic.IEnumerable%601.GetEnumerator%2A> and in the <xref:System.Collections.Generic.IEnumerator%601.Current%2A> property getter. Because `T` is used only in output positions in these members, <xref:System.Collections.Generic.IEnumerable%601> can be marked as covariant. If `T` were used in an input position in an overridden member, the interface couldn't be covariant, and the compiler would generate an error.
3030

3131
Concrete classes can implement closed constructed interfaces, as follows:
3232

0 commit comments

Comments
 (0)