当前位置:网站首页>. Net interface can be implemented by default
. Net interface can be implemented by default
2022-07-07 03:38:00 【Sky watching houses】
from .NET Core 3.0 Upper C# 8.0 Start , You can define implementations when you declare interface members . The most common solution is to safely add members to interfaces that have been published and used by countless clients .
Example :
internal interface ICustomer
{
IEnumerable<IOrder> PreviousOrders {
get; }
DateTime DateJoined {
get; }
DateTime? LastOrder {
get; }
string Name {
get; }
IDictionary<DateTime, string> Reminders {
get; }
}
internal interface IOrder
{
DateTime Purchased {
get; }
decimal Cost {
get; }
}
C# Two common interfaces in . Then came the new feature .
internal interface ICustomer
{
IEnumerable<IOrder> PreviousOrders {
get; }
DateTime DateJoined {
get; }
DateTime? LastOrder {
get; }
string Name {
get; }
IDictionary<DateTime, string> Reminders {
get; }
// Version 1:
public decimal ComputeLoyaltyDiscountVersionOne()
{
DateTime TwoYearsAgo = DateTime.Now.AddYears(-2);
if ((DateJoined < TwoYearsAgo) && (PreviousOrders.Count() > 10))
{
return 0.10m;
}
return 0;
}
// Version 2:
public static void SetLoyaltyThresholds(
TimeSpan ago,
int minimumOrders = 10,
decimal percentageDiscount = 0.10m)
{
length = ago;
orderCount = minimumOrders;
discountPercent = percentageDiscount;
}
private static TimeSpan length = new TimeSpan(365 * 2, 0, 0, 0); // two years
private static int orderCount = 10;
private static decimal discountPercent = 0.10m;
public decimal ComputeLoyaltyDiscountVersionTwo()
{
DateTime start = DateTime.Now - length;
if ((DateJoined < start) && (PreviousOrders.Count() > orderCount))
{
return discountPercent;
}
return 0;
}
// Another way
public decimal ComputeLoyaltyDiscount() => DefaultLoyaltyDiscount(this);
protected static decimal DefaultLoyaltyDiscount(ICustomer c)
{
DateTime start = DateTime.Now - length;
if ((c.DateJoined < start) && (c.PreviousOrders.Count() > orderCount))
{
return discountPercent;
}
return 0;
}
}```
边栏推荐
猜你喜欢

注意力机制原理

22. (ArcGIS API for JS) ArcGIS API for JS Circle Collection (sketchviewmodel)

未来发展路线确认!数字经济、数字化转型、数据...这次会议很重要

QT thread and other 01 concepts

Stored procedures and functions (MySQL)

input_ delay

When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks
![[security attack and Defense] how much do you know about serialization and deserialization?](/img/1c/e5ae74e65bacf688d7f61cc1b71d3e.png)
[security attack and Defense] how much do you know about serialization and deserialization?

存储过程与函数(MySQL)

24. (ArcGIS API for JS) ArcGIS API for JS point modification point editing (sketchviewmodel)
随机推荐
Restcloud ETL Community Edition June featured Q & A
codeforces每日5题(均1700)-第七天
源代码保密的意义和措施
树莓派设置静态ip
Sub pixel corner detection opencv cornersubpix
卡尔曼滤波-1
QT 打开文件 使用 QFileDialog 获取文件名称、内容等
25.(arcgis api for js篇)arcgis api for js线修改线编辑(SketchViewModel)
Shangsilicon Valley JVM Chapter 1 class loading subsystem
Baidu map JS development, open a blank, bmapgl is not defined, err_ FILE_ NOT_ FOUND
MySQL storage engine
哈夫曼树基本概念
【安全攻防】序列化與反序列,你了解多少?
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
About Estimation Statistics
体会设计细节
未来发展路线确认!数字经济、数字化转型、数据...这次会议很重要
Principle of attention mechanism
[colmap] 3D reconstruction with known camera pose
When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks