@@ -263,6 +263,7 @@ The set of members of an interface declared in multiple parts ([§14.2.7](classe
263
263
264
264
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:
265
265
266
+ <!-- Example: {template:"standalone-console", name:"InterfaceMember", expectedOutput:["IB.M", "IA.P = 10", "IB.P = 20"]} -->
266
267
``` csharp
267
268
interface IA
268
269
{
@@ -273,7 +274,7 @@ interface IA
273
274
}
274
275
}
275
276
276
- interface IB : IA
277
+ interface IB : IA
277
278
{
278
279
public new int P { get { return 20 ; } }
279
280
void IA.M()
@@ -282,11 +283,11 @@ interface IB : IA
282
283
}
283
284
}
284
285
285
- class C : IB { }
286
+ class C : IB { }
286
287
287
288
class Test
288
289
{
289
- public static void Start ()
290
+ public static void Main ()
290
291
{
291
292
C c = new C ();
292
293
((IA )c ).M (); // cast needed
@@ -324,11 +325,11 @@ As a static *field_declaration* is considered to have a default implementation (
324
325
325
326
> * Example* : The following program contains static members of various kinds:
326
327
>
328
+ > <!-- Example: {template:"standalone-console", name:"InterfaceFields", inferOutput:true} -->
327
329
> ``` csharp
328
- > using System ;
329
330
> public interface IX
330
331
> {
331
- > private const int constant = 100;
332
+ > public const int constant = 100;
332
333
> protected static int field;
333
334
>
334
335
> static IX()
@@ -338,7 +339,10 @@ As a static *field_declaration* is considered to have a default implementation (
338
339
> field = 50 ;
339
340
> Console .WriteLine (" static constructor has run" );
340
341
> }
341
- >
342
+ > }
343
+ >
344
+ > public class Test : IX
345
+ > {
342
346
> public static void Main ()
343
347
> {
344
348
> Console .WriteLine ($" constant = {IX .constant }, field = {IX .field }" );
@@ -405,9 +409,9 @@ These rules ensure that any covariant or contravariant usage of the interface re
405
409
> < ! -- FIX : need to define I < T > , then perhaps break into 2 files , first one a standalone - lib , the second , code - in - class-lib. -->
406
410
> ```csharp
407
411
> class B {}
408
- > class D : B {}
409
- > class E : B {}
410
- > class C : I <D >
412
+ > class D : B {}
413
+ > class E : B {}
414
+ > class C : I <D >
411
415
> {
412
416
> public void M <U >() {.. .}
413
417
> }
@@ -430,13 +434,14 @@ A virtual method with implementation declared in an interface may be overridden
430
434
431
435
> *Example*:
432
436
>
437
+ > <!-- Example : {template :"standalone -lib -without -using ", name :"InterfaceMethods2 "} -->
433
438
> ```csharp
434
439
> interface IA
435
440
> {
436
441
> void M () { Console .WriteLine (" IA.M" ); }
437
442
> }
438
443
>
439
- > interface IB : IA
444
+ > interface IB : IA
440
445
> {
441
446
> abstract void IA.M(); // reabstraction of M
442
447
> }
@@ -448,13 +453,14 @@ Reabstraction is also permissible in an implementing class.
448
453
449
454
> *Example *:
450
455
>
456
+ > < ! -- Example : {template : " standalone-lib-without-using" , name : " InterfaceMethods3" } -- >
451
457
> ```csharp
452
458
> interface I1
453
459
> {
454
460
> void M () { }
455
461
> }
456
462
>
457
- > abstract class C : I1
463
+ > abstract class C : I1
458
464
> {
459
465
> public abstract void M (); // implement I1.M with an abstract method in C
460
466
> }
@@ -471,6 +477,7 @@ One override `M1` is considered *more specific* than another override `M2` if `M
471
477
472
478
> *Example*:
473
479
>
480
+ > <!-- Example : {template :"standalone -lib -without -using ", name :"InterfaceMethods4 ", expectedErrors :["CS8705"]} -->
474
481
> ```csharp
475
482
> interface IA
476
483
> {
@@ -482,14 +489,14 @@ One override `M1` is considered *more specific* than another override `M2` if `M
482
489
> void IA.M() { Console.WriteLine("IB.M"); }
483
490
> }
484
491
>
485
- > interface IC : IA
492
+ > interface IC : IA
486
493
> {
487
494
> void IA.M() { Console.WriteLine("IC.M"); }
488
495
> }
489
496
>
490
- > abstract class C : IB , IC { } // error: no most specific override for 'IA.M'
497
+ > abstract class C : IB , IC { } // error: no most specific override for 'IA.M'
491
498
>
492
- > abstract class D : IA , IB , IC // OK
499
+ > abstract class D : IA , IB , IC // OK
493
500
> {
494
501
> public abstract void M ();
495
502
> }
@@ -501,6 +508,7 @@ It is an error if in a class declaration the most specific override of some inte
501
508
502
509
> *Example *:
503
510
>
511
+ > < ! -- Example : {template : " standalone-lib-without-using" , name : " InterfaceMethods5" , expectedErrors : [" CS0535" ]} -- >
504
512
> ```csharp
505
513
> interface IF
506
514
> {
@@ -587,6 +595,7 @@ It is an error to declare a class type, struct type, or enum type within the sco
587
595
588
596
> *Example*: The declaration of `C ` below is an error .
589
597
>
598
+ > <!-- Example : {template :"standalone -lib -without -using ", name :"InterfaceNestedTypes ", expectedErrors :["CS8427"]} -->
590
599
> ```csharp
591
600
> interface IOuter <out T >
592
601
> {
0 commit comments