@@ -260,6 +260,7 @@ The set of members of an interface declared in multiple parts ([§14.2.7](classe
260
260
261
261
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:
262
262
263
+ <!-- Example: {template:"standalone-console", name:"InterfaceMember", expectedOutput:["IB.M", "IA.P = 10", "IB.P = 20"]} -->
263
264
``` csharp
264
265
interface IA
265
266
{
@@ -270,7 +271,7 @@ interface IA
270
271
}
271
272
}
272
273
273
- interface IB : IA
274
+ interface IB : IA
274
275
{
275
276
public new int P { get { return 20 ; } }
276
277
void IA.M()
@@ -279,11 +280,11 @@ interface IB : IA
279
280
}
280
281
}
281
282
282
- class C : IB { }
283
+ class C : IB { }
283
284
284
285
class Test
285
286
{
286
- public static void Start ()
287
+ public static void Main ()
287
288
{
288
289
C c = new C ();
289
290
((IA )c ).M (); // cast needed
@@ -321,11 +322,11 @@ As a static *field_declaration* is considered to have a default implementation (
321
322
322
323
> * Example* : The following program contains static members of various kinds:
323
324
>
325
+ > <!-- Example: {template:"standalone-console", name:"InterfaceFields", inferOutput:true} -->
324
326
> ``` csharp
325
- > using System ;
326
327
> public interface IX
327
328
> {
328
- > private const int constant = 100;
329
+ > public const int constant = 100;
329
330
> protected static int field;
330
331
>
331
332
> static IX()
@@ -335,7 +336,10 @@ As a static *field_declaration* is considered to have a default implementation (
335
336
> field = 50 ;
336
337
> Console .WriteLine (" static constructor has run" );
337
338
> }
338
- >
339
+ > }
340
+ >
341
+ > public class Test : IX
342
+ > {
339
343
> public static void Main ()
340
344
> {
341
345
> 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
386
390
387
391
> *Example *:
388
392
>
393
+ > <!-- Example : {template :"standalone -lib -without -using ", name :"InterfaceMethods1 ", expectedErrors :["CS1961"]} -->
389
394
> ```csharp
390
395
> interface I <out T >
391
396
> {
@@ -397,11 +402,12 @@ These rules ensure that any covariant or contravariant usage of the interface re
397
402
>
398
403
> Were this restriction not in place it would be possible to violate type safety in the following manner :
399
404
>
405
+ > < ! -- NotAn $Example : {} -- >
400
406
> ```csharp
401
407
> 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 >
405
411
> {
406
412
> public void M <U >() {.. .}
407
413
> }
@@ -424,13 +430,14 @@ A virtual method with implementation declared in an interface may be overridden
424
430
425
431
> *Example*:
426
432
>
433
+ > <!-- Example : {template :"standalone -lib -without -using ", name :"InterfaceMethods2 "} -->
427
434
> ```csharp
428
435
> interface IA
429
436
> {
430
437
> void M () { Console .WriteLine (" IA.M" ); }
431
438
> }
432
439
>
433
- > interface IB : IA
440
+ > interface IB : IA
434
441
> {
435
442
> abstract void IA.M(); // reabstraction of M
436
443
> }
@@ -442,13 +449,14 @@ Reabstraction is also permissible in an implementing class.
442
449
443
450
> *Example *:
444
451
>
452
+ > < ! -- Example : {template : " standalone-lib-without-using" , name : " InterfaceMethods3" } -- >
445
453
> ```csharp
446
454
> interface I1
447
455
> {
448
456
> void M () { }
449
457
> }
450
458
>
451
- > abstract class C : I1
459
+ > abstract class C : I1
452
460
> {
453
461
> public abstract void M (); // implement I1.M with an abstract method in C
454
462
> }
@@ -465,6 +473,7 @@ One override `M1` is considered *more specific* than another override `M2` if `M
465
473
466
474
> *Example*:
467
475
>
476
+ > <!-- Example : {template :"standalone -lib -without -using ", name :"InterfaceMethods4 ", expectedErrors :["CS8705"]} -->
468
477
> ```csharp
469
478
> interface IA
470
479
> {
@@ -476,14 +485,14 @@ One override `M1` is considered *more specific* than another override `M2` if `M
476
485
> void IA.M() { Console.WriteLine("IB.M"); }
477
486
> }
478
487
>
479
- > interface IC : IA
488
+ > interface IC : IA
480
489
> {
481
490
> void IA.M() { Console.WriteLine("IC.M"); }
482
491
> }
483
492
>
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'
485
494
>
486
- > abstract class D : IA , IB , IC // OK
495
+ > abstract class D : IA , IB , IC // OK
487
496
> {
488
497
> public abstract void M ();
489
498
> }
@@ -495,6 +504,7 @@ It is an error if in a class declaration the most specific override of some inte
495
504
496
505
> *Example *:
497
506
>
507
+ > < ! -- Example : {template : " standalone-lib-without-using" , name : " InterfaceMethods5" , expectedErrors : [" CS0535" ]} -- >
498
508
> ```csharp
499
509
> interface IF
500
510
> {
@@ -581,6 +591,7 @@ It is an error to declare a class type, struct type, or enum type within the sco
581
591
582
592
> *Example*: The declaration of `C ` below is an error .
583
593
>
594
+ > <!-- Example : {template :"standalone -lib -without -using ", name :"InterfaceNestedTypes ", expectedErrors :["CS8427"]} -->
584
595
> ```csharp
585
596
> interface IOuter <out T >
586
597
> {
0 commit comments