Beginning with C# 8.0, you can define an implementation when you declare a member of an interface.
What is the default interfaces?
An interface that permits the interface to showcase a “default” implementation for the method in classes that do not provide an overriding implementation.
Example
The primary example of default interface in C# 8.0
interface IExample {
void Print() { WriteLine("Hello World"); }
}
In this tutorial, you’ll learn how to:
Extend interfaces safely by adding default interface methods.
Let us take an example of a “Library” Application that provided minimal interface definitions for users like Customers of their library to implement. The primary aspect is to create a better relationship with existing customers and improve their relationships with new customers.
ICustomer interface
public interface ICustomer {
IEnumerable PastOrders { get; }
DateTime DtCreated { get; }
DateTime? LastOrder { get; }
string Name { get; }
IDictionary<DateTime,string> Reminders { get; }
}
IOrder interface
public interface IOrder {
DateTime DtPurchased { get; }
decimal TotalPrice { get; }
}
Now the company wants to add a Loyalty Discount for the customers who has a lot of orders. Each implementation of the ICustomer can set different rules for the loyalty discount.
The ideal approach is to develop an interface method, but considering a lot of implementations of the above interface, it becomes a challenging & code breaker task. C# 8.0 allows you to add default implementation of this new method, overridden if required.
Customers or Users can accept the default implementation as a non-breaking change. If their business rules are different, they can override.
Upgrade ICutomer with default interface methods
New requirements for the loyalty program eligibility
The customer has more than two past orders.
The customer account should be at least two years old.
It’s a classic situation for default interface methods. You can add a method to the ICustomer interface and provide the most suitable implementation. All existing, and any further implementations can practice the default implementation or implement their own.
public interface ICustomer
{
List PastOrders { get; }
DateTime DtCreated { get; }
DateTime? LastOrder { get; }
string Name { get; }
IDictionary<DateTime, string> Reminders { get; }
public decimal ComputeLoyaltyDiscount()
{
DateTime TwoYearsAgo = DateTime.Now.AddYears(-2);
if ((DtCreated<TwoYearsAgo) && (PastOrders.Count() > 1))
{
return 0.10m;
}
return 0;
}
}
Let’s create sample customer and order classes that implement ICustomer & IOrder interfaces, respectively.
Sample Customer class
Please find below the sample customer class which implements the ICustomer interface. Notice that the “ComputeLoyaltyDiscount” method by default not implemented.
public class SampleCustomer : ICustomer
{
private string _name;
private DateTime _dtCreated;
private IDictionary<DateTime, string> _reminders;
private List _pastOrders;
public SampleCustomer(string name, DateTime dtCreated, IDictionary<DateTime, string> reminders)
{
_name = name;
_dtCreated = dtCreated;
_reminders = reminders;
_pastOrders = new List();
}
List ICustomer.PastOrders => _pastOrders;
DateTime ICustomer.DtCreated => _dtCreated;
DateTime? ICustomer.LastOrder => null;
string ICustomer.Name => _name;
IDictionary<DateTime, string> ICustomer.Reminders => _reminders;
internal void AddOrder(SampleOrder o)
{
_pastOrders.Add(o);
}
}
Sample Order class
Please find below the sample order class, which implements the IOrder interface.
public class SampleOrder : IOrder
{
private DateTime _dateTime;
private decimal _price;
public SampleOrder(DateTime dateTime, decimal value)
{
_dateTime = dateTime;
_price = value;
}
public DateTime DtPurchased => _dateTime;
public decimal TotalPrice => _price;
}
Calculate Loyalty Discount
Please find below the data preparation to Compute the Loyalty Discount.
IDictionary<DateTime, string> reminders = new Dictionary<DateTime, string>();
reminders.Add(new DateTime(2010, 08, 12), "childs's birthday");
reminders.Add(new DateTime(1012, 11, 15), "anniversary");
SampleCustomer c = new SampleCustomer("customer one", new DateTime(2010, 5, 31),reminders);
SampleOrder o = new SampleOrder(new DateTime(2012, 6, 1), 5m);
c.AddOrder(o);
o = new SampleOrder(new DateTime(2103, 7, 4), 25m);
c.AddOrder(o);
ICustomer theCustomer = c;
Console.WriteLine($"Offer: {theCustomer.ComputeLoyaltyDiscount()}");
Console.ReadLine();
Console Output
Offer: 0.10
Github Repo
More C# 8.0 Concepts
Advance Property Pattern C# 8.0
Null-Coalescing assignment C# 8.0
Thank you for reading. Keep visiting and share this in your network. Please put your thoughts and feedback in the comments section.