Tech Point Fundamentals

Saturday, August 22, 2020

Interface default method | Default Interface Method | C# 8

Can an interface have method definition in C#?


Until C# 7.3 we don’t have any ability to define the method definition, but in C# 8 we have the ability to have default method implementation.


Watch our videos here




In Visual Studio 2017 we have C# till version 7.3, but in Visual Studio 2019, Microsoft has introduced C# 8. 


How to check C# version in Visual Studio

What's new in C# 8


The C# language versions are more closely to .Net Framework version, so C# 8 is only available with .Net Core 3.x and .Net Standard 2.1. Both are available in Visual Studio 2019.



Coming to the point, yes, you can have concrete method definitions in an interface in C# 8. You can now add members to interfaces and provide an implementation for those members.

Default Interface Methods

Default interface methods are also known as Virtual Extension Methods, which will allow C# developers to use the Traits Programming Technique. Traits are object-oriented programming technology that promotes the reuse of methods between unrelated classes. We can now have default method definitions in the interface.



Example

Here is a simple console application program which demonstrates the default method definition of the interface:




 

interface ICustomer

{

public string GetCustomerName()

{

     return "Tech Point Fundamentals";

}

}

 

class Customer : ICustomer

{

public int GetCustomerId()

{

     return 1;

}

}

 

public class Program

{

public static void Main()

{  

     ICustomer _customer = new Customer();

     Console.WriteLine(_customer.GetCustomerName());        

}

}


Console Output:

Tech Point Fundamentals



Points To Remember

  1. The inheriting class does not know anything about the default method. Therefore it can not have any implementation for that interface method. So the class that implements this interface need not implement its concrete method.


  1. The inheriting class does not inherit any default members from its inherited interfaces at all. So you can not call the default methods of any interface by class object directly.


Let me clear you the above point more. The following code will produce a compile-time error: 'Customer' does not contain a definition for 'GetCustomerName'

Customer customer = new Customer();

customer.GetCustomerName();


To access the default method of the interface by the class object, you must upcast it to the interface:


Customer customer = new Customer();

((ICustomer)customer).GetCustomerName();


  1. The good point is that you can add new functionality to the interface without breaking the compatibility with the older versions of those interfaces.


  1. The same feature has existed in Java for a long time.




Video Recommendation



Watch More Videos...

8 comments:

  1. Can you have access modifiers also in an interface?

    ReplyDelete
    Replies
    1. Yes. Please visit the following article:

      https://www.techpointfunda.com/2020/08/interface-access-modifiers-csharp.html

      Delete
  2. Private access modifiers are also allowed in interface?

    ReplyDelete
    Replies
    1. Yes, you can have private access modifiers also. Please visit the following article:

      https://www.techpointfunda.com/2020/08/interface-access-modifiers-csharp.html

      Delete
  3. diff bet

    api/home/getcust?=1
    and
    api/home/getcust/1

    ReplyDelete
    Replies
    1. The URI Parameter or path parameter (/id) is basically used to identify a specific resource while the query parameter (?id) is used to filter the resource.

      Delete
    2. Please watch this video for more details:

      https://youtu.be/-ViYPvycKwc

      Delete

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