- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why cannot we specify access modifiers inside an interface in C#?
Interface methods are contract with the outside world which specifies that class implementing this interface does a certain set of things.
Interface members are always public because the purpose of an interface is to enable other types to access a class or struct.
Interfaces can have access specifiers like protected or internal etc. Thus limiting 'the outside world' to a subset of 'the whole outside world'.
Example
interface IInterface{ void Save(); } class Program{ static void Main(){ Console.ReadLine(); } }
The above example will compile properly without any errors
Prior to C# 8, interface members were public by default. In fact, if you put an access modifier on an interface member (including public), it would generate a compiler error.
interface IInterface{ Public void Save(); } class Program{ static void Main(){ Console.ReadLine(); } }
The above code throws compile time error in c# 7.0,but in c# 8.0 it compiles without any error
- Related Articles
- Why an interface cannot implement another interface in Java?
- Access Modifiers in C++
- Access Modifiers in C#
- Why do we use modifiers in C/C++?
- Can we define constructor inside an interface in java?
- What are access modifiers and non-access modifiers in Java?
- What are access modifiers in C++?
- Can we define an interface inside a Java class?
- Access Modifiers in Java
- Access and Non Access Modifiers in Java
- What are the differences between access modifiers and non-access modifiers in Java?
- Non Access Modifiers in Java
- Types of access modifiers in Java?
- What are different types of access modifiers available in C#?
- What are the modifiers allowed for methods in an Interface in java?

Advertisements