Can an interface have method definition in C#?
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
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
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.
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();
The good point is that you can add new functionality to the interface without breaking the compatibility with the older versions of those interfaces.
The same feature has existed in Java for a long time.
Good article
ReplyDeleteCan you have access modifiers also in an interface?
ReplyDeleteYes. Please visit the following article:
Deletehttps://www.techpointfunda.com/2020/08/interface-access-modifiers-csharp.html
Private access modifiers are also allowed in interface?
ReplyDeleteYes, you can have private access modifiers also. Please visit the following article:
Deletehttps://www.techpointfunda.com/2020/08/interface-access-modifiers-csharp.html
diff bet
ReplyDeleteapi/home/getcust?=1
and
api/home/getcust/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.
DeletePlease watch this video for more details:
Deletehttps://youtu.be/-ViYPvycKwc