Skip to content

Commit 84a1703

Browse files
authored
add annotation to new examples
1 parent 35f190d commit 84a1703

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

standard/interfaces.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ The set of members of an interface declared in multiple parts ([§14.2.7](classe
260260

261261
Consider an interface with a default implementation for a member `M`. As `M` is not part of that interface’s contract, outside that interface or any interface derived from it, that name is not visible. How then can it be accessed? The following code shows how:
262262

263+
<!-- Example: {template:"standalone-console", name:"InterfaceMember", expectedOutput:["IB.M", "IA.P = 10", "IB.P = 20"]} -->
263264
```csharp
264265
interface IA
265266
{
@@ -270,7 +271,7 @@ interface IA
270271
}
271272
}
272273

273-
interface IB : IA
274+
interface IB: IA
274275
{
275276
public new int P { get { return 20; } }
276277
void IA.M()
@@ -279,11 +280,11 @@ interface IB : IA
279280
}
280281
}
281282

282-
class C : IB { }
283+
class C: IB { }
283284

284285
class Test
285286
{
286-
public static void Start()
287+
public static void Main()
287288
{
288289
C c = new C();
289290
((IA)c).M(); // cast needed
@@ -321,11 +322,11 @@ As a static *field_declaration* is considered to have a default implementation (
321322

322323
> *Example*: The following program contains static members of various kinds:
323324
>
325+
> <!-- Example: {template:"standalone-console", name:"InterfaceFields", inferOutput:true} -->
324326
> ```csharp
325-
> using System;
326327
> public interface IX
327328
> {
328-
> private const int constant = 100;
329+
> public const int constant = 100;
329330
> protected static int field;
330331
>
331332
> static IX()
@@ -335,7 +336,10 @@ As a static *field_declaration* is considered to have a default implementation (
335336
> field = 50;
336337
> Console.WriteLine("static constructor has run");
337338
> }
338-
>
339+
> }
340+
>
341+
> public class Test: IX
342+
> {
339343
> public static void Main()
340344
> {
341345
> Console.WriteLine($"constant = {IX.constant}, field = {IX.field}");
@@ -386,6 +390,7 @@ These rules ensure that any covariant or contravariant usage of the interface re
386390
387391
> *Example*:
388392
>
393+
> <!-- Example: {template:"standalone-lib-without-using", name:"InterfaceMethods1", expectedErrors:["CS1961"]} -->
389394
> ```csharp
390395
> interface I<out T>
391396
> {
@@ -397,11 +402,12 @@ These rules ensure that any covariant or contravariant usage of the interface re
397402
>
398403
> Were this restriction not in place it would be possible to violate type safety in the following manner:
399404
>
405+
> <!-- NotAn$Example: {} -->
400406
> ```csharp
401407
> class B {}
402-
> class D : B {}
403-
> class E : B {}
404-
> class C : I<D>
408+
> class D: B {}
409+
> class E: B {}
410+
> class C: I<D>
405411
> {
406412
> public void M<U>() {...}
407413
> }
@@ -424,13 +430,14 @@ A virtual method with implementation declared in an interface may be overridden
424430
425431
> *Example*:
426432
>
433+
> <!-- Example: {template:"standalone-lib-without-using", name:"InterfaceMethods2"} -->
427434
> ```csharp
428435
> interface IA
429436
> {
430437
> void M() { Console.WriteLine("IA.M"); }
431438
> }
432439
>
433-
> interface IB : IA
440+
> interface IB: IA
434441
> {
435442
> abstract void IA.M(); // reabstraction of M
436443
> }
@@ -442,13 +449,14 @@ Reabstraction is also permissible in an implementing class.
442449
443450
> *Example*:
444451
>
452+
> <!-- Example: {template:"standalone-lib-without-using", name:"InterfaceMethods3"} -->
445453
> ```csharp
446454
> interface I1
447455
> {
448456
> void M() { }
449457
> }
450458
>
451-
> abstract class C : I1
459+
> abstract class C: I1
452460
> {
453461
> public abstract void M(); // implement I1.M with an abstract method in C
454462
> }
@@ -465,6 +473,7 @@ One override `M1` is considered *more specific* than another override `M2` if `M
465473
466474
> *Example*:
467475
>
476+
> <!-- Example: {template:"standalone-lib-without-using", name:"InterfaceMethods4", expectedErrors:["CS8705"]} -->
468477
> ```csharp
469478
> interface IA
470479
> {
@@ -476,14 +485,14 @@ One override `M1` is considered *more specific* than another override `M2` if `M
476485
> void IA.M() { Console.WriteLine("IB.M"); }
477486
> }
478487
>
479-
> interface IC : IA
488+
> interface IC: IA
480489
> {
481490
> void IA.M() { Console.WriteLine("IC.M"); }
482491
> }
483492
>
484-
> abstract class C : IB, IC { } // error: no most specific override for 'IA.M'
493+
> abstract class C: IB, IC { } // error: no most specific override for 'IA.M'
485494
>
486-
> abstract class D : IA, IB, IC // OK
495+
> abstract class D: IA, IB, IC // OK
487496
> {
488497
> public abstract void M();
489498
> }
@@ -495,6 +504,7 @@ It is an error if in a class declaration the most specific override of some inte
495504
496505
> *Example*:
497506
>
507+
> <!-- Example: {template:"standalone-lib-without-using", name:"InterfaceMethods5", expectedErrors:["CS0535"]} -->
498508
> ```csharp
499509
> interface IF
500510
> {
@@ -581,6 +591,7 @@ It is an error to declare a class type, struct type, or enum type within the sco
581591
582592
> *Example*: The declaration of `C` below is an error.
583593
>
594+
> <!-- Example: {template:"standalone-lib-without-using", name:"InterfaceNestedTypes", expectedErrors:["CS8427"]} -->
584595
> ```csharp
585596
> interface IOuter<out T>
586597
> {

0 commit comments

Comments
 (0)