Can an Interface have Access Modifiers in C#?
The interface is the most important topic for every developer and interviewee. Since the birth of C# 8 and various .NET implementations and .NET Standard, there has been a lot of confusion regarding this topic in the development community. Today we will try to understand this topic in a simple and understandable way.
Today’s Agenda
Can you have explicit access modifiers for the interface members in C#?
Can you have private interface members in C#?
Can you have private interface members without default implementations in C#?
What is the use of private members in any interface?
What is the default access level of interface members in C#?
Can you have protected interface members in C#?
Can you implement non-public interface members in inheriting class?
Can you access internal or protected internal interface members from the inheriting class reference variable?
Can you access internal or protected internal interface members from the interface reference variable?
Can protected interface members be accessed by class or interface reference variable?
There are a lot of new features for the interface in C# 8 which are not there in earlier versions of C#. Some of them we will look at in this article.
Introduction
Microsoft has introduced C# 8 with .Net Core 3.x in Visual studio 2019. In C# 8 Microsoft has introduced a lot of new features for the interface as well. C# 8 is only available in .Net Standard 2.1 and .Net Core 3.x implements the same. The C# syntax for an interface is extended to accept more keywords like public, private, protected, internal, protected internal.
How to check C# version in Visual Studio
How to check the .Net Framework version in Visual Studio
What is .Net Standard
.Net Standard is not any implementation of .Net. It is a formal specification which defines the set of APIs that all .Net implementations must provide. The idea behind .Net Standard is to establish greater uniformity in the .Net ecosystem. The various .Net implementations target specific versions of .Net Standard.
What is .Net Core
.Net Core is the latest implementation of .Net which runs on Windows, Linux, and macOS.
.Net Core is an open-source and cross-platform implementation of .Net. The .Net Core 3.x is a framework with a runtime that implements .Net Standard 2.1.There are six access modifiers in C# called public, private, protected, internal, protected internal, and private protected. The last one i.e. private protected is introduced in C# version 7.2 and it is only valid in C# version 7.2 and later.
A default access level is used if no access modifier is specified in a member or type declaration.
For some user-defined types or type members who have a default access level, you can’t specify them explicitly at all. For example interface and enum members are always public and no access modifiers are allowed. If you are trying to add any access modifiers explicitly, it causes a compile-time error.
Yes, you can have all explicit access modifiers like public, private, protected, internal, protected internal for all member types of interface like method, properties, and indexers in C# 8.
In the above example interface “ICustomer” we have members with different access modifiers.
But you should remember that in earlier versions of C# you can’t have any explicit access modifier to any member of the interface. All are public by default. It is a compile-time error if you try to add any access modifiers, even public to any interface member.
Can you have private interface members in C#?
Yes, you can have private interface members in C# 8. But remember that if you are making any member
as private then, you have to provide a default implementation for that.
In the above example, we have defined “CustomerId” as private and also defined the default property
implementation.
But you should remember that in earlier versions of C# you can’t have any private members at all in
the interface. All are public by default.
What is default methods in C# 8 interface
No. It is a compile-time error if you are not providing the default implementation to any private members. It makes sense actually, we know that private members can’t be accessed outside the type where they are declared or defined.
In the above example, if you do not define the private “CustomerId” property with default then it produces a compile-time error.
What is default implementation in C# 8 interface
What is the use of private members in an interface?
Every private member has its default implementation in C# 8, so they can be used within any other default implementations. It is a compile-time error for a private member of an interface to have no definition body.
In the above example private “CustomerId” property is used in the default implementation of “GetCustomerId()”.
What is the default access level of interface members in C#?
The default access level is the public, but it may be specified explicitly in C# 8.
In the above example “GetCustomerAddress()” method has the default access level which is public.
Only public members can be implemented in inheriting class implicitly, and “GetCustomerAddress()” is implemented in
the class “Customer” implicitly and also accessed from the reference variable of customer class which is “_customer”.
Also notice that we have marked “GetCustomerName()” as public explicitly in the above example.
But you should remember that in earlier versions of C# you can’t explicitly decorate any interface member with any access modifier, even public as well, they all are public by default. If you try to do so, it will produce a compile-time error
.
Can you have protected interface members in C#?
Yes, you can have protected interface members in C# 8. But remember that if you are making any member
as protected then, only the inheriting interface can access or implement it. The inheriting class can’t
implement it implicitly, it can only be explicitly implemented.
In the above example, we have declared “GetCustomerSecondaryPhoneNo()” as a protected member
in the interface “ICustomer”.
You can see that we can access the protected member “GetCustomerPrimaryPhoneNo()” in
“GetRemoteCustomerPrimaryPhoneNo()” method of the inheriting interface “IRemoteCustomer”.
We have also implemented the protected method “GetCustomerSecondaryPhoneNo()” in the inheriting
class “Customer”.
One point should be remembered here, that you can’t access any protected member from either by
the class reference variable or by the interface reference variable itself.
In the above example, you can’t access “GetCustomerPrimaryPhoneNo()” from the reference variable
of the interface “ICustomer” ie “_regularCustomer”. Similarly, you can’t access
“GetCustomerSecondaryPhoneNo()” from the reference variable of the interface
“IRemoteCustomer” ie “_remoteCustomer”. It will produce a compile-time error if you try to do so.
But you should remember that in earlier versions of C# you can’t have any private members at all in the interface. All are public by default.
Can you implement non-public interface members in the inheriting class?
Not Implicitly, but Explicitly. Non-Public members can’t be implemented in any inheriting class
implicitly, you have to do it by the explicit implementation. Remember that access modifier is not
allowed on explicitly implemented interface members.
You can implement internal, protected, and protected internal members in the inheriting class,
but private members can’t be implemented in either way.
You can see in the example we have implemented an internal member
“GetCustomerSecondaryAddress()” and protected member “GetCustomerSecondaryPhoneNo()”
in inheriting class “Customer”.
You should remember that you can’t access any explicitly implemented member by class reference
variable directly. But with interface reference variables you can access internal and protected internal
members only, not protected members.
In the above example you can’t access “GetCustomerPrimaryPhoneNo()” and “GetCustomerSecondaryPhoneNo()” with the “_regularCustomer” variable.
Can you access internal or protected internal interface members from the inheriting class reference variable?
No, you can’t access any internal or protected internal interface member from the class reference variable.
You can only access it by its interface reference variable.
In the above example you can’t access “GetCustomerSecondaryEmail()” and “GetCustomerOrderId()” method from class reference variable “_customer”. But you can access the same by interface reference variable “_regularCustomer”.
Can you access internal or protected internal interface members from the interface reference variable?
Yes, you can access any internal or protected internal interface member from the interface reference variable.
In the above example you can access “GetCustomerSecondaryEmail()” and “GetCustomerOrderId()” methods by interface reference variable “_regularCustomer”.
Can protected interface members be accessed by class or interface reference variable?
No, you can’t access any protected interface member either by class reference variable or interface
reference variable. The protected members can only be accessed within the inheriting interface.
In the above example you can’t access “GetCustomerPrimaryPhoneNo()” or
“GetCustomerSecondaryPhoneNo()” method by interface reference variable “_regularCustomer”
or class reference variable “_customer”.
Live Demo
Conclusion
You can have explicit access modifiers for interface members.
You can also have private interface members with default implementations.
The default access level is public but you can also specify explicitly.
You can also have protected, internal, and protected internal interface members.
Only public interface members can be implemented implicitly in the inheriting class.
The non-public interface members can only be implemented explicitly in the inheriting class.
The non-public members can’t be accessed by class reference variables at all.
The protected members can’t be accessed even by interface reference variables.
The protected members can be accessed in the inheriting interface.
You can’t access any interface member having default implementation by the class reference variable.
In earlier versions of C#:
You can not have any explicit access modifiers to any interface member.
All interface members are public by default.
You can’t specify even public explicitly to any interface member.
Inheriting class has to implement all the interface members.
No comments:
Post a Comment
Please do not enter any HTML. JavaScript or spam link in the comment box.