Tech Point Fundamentals

Sunday, August 30, 2020

Interface Access Modifiers | C# 8

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. 


Watch our videos here


Today’s Agenda

  1. Can you have explicit access modifiers for the interface members in C#?

  2. Can you have private interface members in C#?

  3. Can you have private interface members without default implementations in C#?

  4. What is the use of private members in any interface?

  5. What is the default access level of interface members in C#?

  6. Can you have protected interface members in C#?

  7. Can you implement non-public interface members in inheriting class?

  8. Can you access internal or protected internal interface members from the inheriting class reference variable?

  9. Can you access internal or protected internal interface members from the interface reference variable?

  10. 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.


What’s new in C# 8

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 Framework

.Net Framework is the original implementation of .Net which was developed by Microsoft to build Web and Desktop applications for Windows only. When .Net Framework was released, it served as a general application development platform for Windows desktop and server-based environments.



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.

What is Access Modifier

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.


The default access modifier for certain C# types are: enum: The default and only access modifier supported is public. class: The default access for a class is private. But you may explicitly define any valid access modifier. interface: The default and only access modifier supported is public. struct: The default access is private but public and internal can also be specified.

Now, before going forward to the answers of the above questions let’s have a quick sample program, to understand the questions in a better way.






Can you have explicit access modifiers for the interface members in C#?

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




Can you have private interface members without default implementation in C#?

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

In C# 8:

  1. You can have explicit access modifiers for interface members. 

  2. You can also have private interface members with default implementations.

  3. The default access level is public but you can also specify explicitly.

  4. You can also have protected, internal, and protected internal interface members.

  5. Only public interface members can be implemented implicitly in the inheriting class. 

  6. The non-public interface members can only be implemented explicitly in the inheriting class.

  7. The non-public members can’t be accessed by class reference variables at all.

  8. The protected members can’t be accessed even by interface reference variables.

  9. The protected members can be accessed in the inheriting interface.

  10. You can’t access any interface member having default implementation by the class reference variable.




In earlier versions of C#:


  1. You can not have any explicit access modifiers to any interface member.

  2. All interface members are public by default.

  3. You can’t specify even public explicitly to any interface member. 

  4. Inheriting class has to implement all the interface members.


Thanks for visiting this page. Please follow us on Twitter, Facebook, LinkedIn and Pinterest for
regular updates.


Video Recommendation



Watch More Videos...

No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.