当前位置:网站首页>. 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;
}
}```
边栏推荐
- 如何自定义Latex停止运行的快捷键
- Probability formula
- Ubuntu20 installation redisjson record
- 如何替换模型的骨干网络(backbone)
- 卡尔曼滤波-1
- Sub pixel corner detection opencv cornersubpix
- 编译常量、ClassLoader类、系统类加载器深度探析
- 哈夫曼树基本概念
- Clock in during winter vacation
- 21. (article ArcGIS API for JS) ArcGIS API for JS rectangular acquisition (sketchviewmodel)
猜你喜欢
Graphical tools package yolov5 and generate executable files exe
24. (ArcGIS API for JS) ArcGIS API for JS point modification point editing (sketchviewmodel)
数学归纳与递归
[security attack and Defense] how much do you know about serialization and deserialization?
23.(arcgis api for js篇)arcgis api for js椭圆采集(SketchViewModel)
What is Ba? How about Ba? What is the relationship between Ba and Bi?
如何替换模型的骨干网络(backbone)
Calculation of time and space complexity (notes of runners)
VHDL implementation of arbitrary size matrix addition operation
VHDL实现任意大小矩阵加法运算
随机推荐
The latest 2022 review of "small sample deep learning image recognition"
25. (ArcGIS API for JS) ArcGIS API for JS line modification line editing (sketchviewmodel)
22.(arcgis api for js篇)arcgis api for js圆采集(SketchViewModel)
24. (ArcGIS API for JS) ArcGIS API for JS point modification point editing (sketchviewmodel)
Probability formula
如何替换模型的骨干网络(backbone)
Ubuntu 20 installation des enregistrements redisjson
Under the tide of "going from virtual to real", Baidu AI Cloud is born from real
Experience design details
[security attack and Defense] how much do you know about serialization and deserialization?
21.(arcgis api for js篇)arcgis api for js矩形采集(SketchViewModel)
小程序能运行在自有App中,且实现直播和连麦?
Leetcode-02 (linked list question)
21. (article ArcGIS API for JS) ArcGIS API for JS rectangular acquisition (sketchviewmodel)
Clock in during winter vacation
Mathematical induction and recursion
Open3d mesh filtering
VHDL implementation of single cycle CPU design
Depth analysis of compilation constants, classloader classes, and system class loaders
Enumeration general interface & enumeration usage specification