5

What is the access modifier of interface methods? It should be public or protected because you have access to them when you implement them (which makes sense). It also should be abstract because they don't have implementation. But lately I've been reading a book called CLR Via C# and the chapter about interfaces says the following

The CLR requires that interface methods be marked as virtual. If you do not explicitly mark the method as virtual in your source code, the compiler marks the method as virtual and sealed.

When you mark the interface member virtual compiler complains that the access modifier in not valid. I mean no access modifier is valid for anything in interface rather than the default one which is given to them by compiler right? Can anyone make it clear for me?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Dimitri
  • 2,798
  • 7
  • 39
  • 59
  • 1
    There is a difference between the implementation details of the CLR and the specified behavior of the C# language. Not all that surprising. The CLR requires that interface methods be virtual, but you don't declare them that way in C# because you can't override them in a derived class. – Cody Gray - on strike Jul 11 '13 at 08:55

2 Answers2

9

Interfaces (C# Programming Guide)

Interfaces can contain methods, properties, events, indexers, or any combination of those four member types. For links to examples, see Related Sections. An interface can't contain constants, fields, operators, instance constructors, destructors, or types. Interface members are automatically public, and they can't include any access modifiers. Members also can't be static.

And about interface members implementation:

To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.

So you can't implement an interface member using protected one.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
0

Don't agree with your comment:

The CLR requires that interface methods be marked as virtual. If you do not explicitly mark the method as virtual in your source code, the compiler marks the method as virtual and sealed.

esp because you mentioned CLR Via C#, I am sure you have mis-read. Bible can not be wrong.

Edit: Providing more details.

As suggested by others, you can not have a access modifier (or virtual) in an interface member. By default (and by definition) all members inside an Interface are public and abstract.

Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
  • I copied the thing you see from the book. How could I miss-read it ? – Dimitri Jul 11 '13 at 08:53
  • 3
    That's not *his* comment, that's a quote from the book you're calling the "bible". Even if it weren't, this isn't a very helpful answer. You don't explain anything, you just [appeal to authority](https://en.wikipedia.org/wiki/Argument_from_authority). Jeffrey Richter is human, of course, he could have made a mistake. – Cody Gray - on strike Jul 11 '13 at 08:57
  • what is wrong with you people? did I do something wrong? U just asked a question. The book said different thing than I knew and I got confused. If you could answer my question then you're welcome and if you could not then why are you here anyway ? – Dimitri Jul 11 '13 at 10:13