把代码放在哪里?(default-interface-methods-versions)

Jiale Xue - MSFT 37,136 信誉分 Microsoft 供应商
2024-04-09T06:17:17.5333333+00:00

我遵循了“C#教程”(https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/default-interface-methods-versions)一书。 由于书中没有提到代码应该放在哪里,我只是将代码粘贴到一个新的标签“customer-relationship.cs”上。

51592-setloyalty.gif

其他代码如下。

ICustomer.cs

using System;  
using System.Collections.Generic;  
  
namespace customer_relationship  
{  
    // <SnippetICustomerVersion1>  
    public interface ICustomer  
    {  
        IEnumerable<IOrder> PreviousOrders { get; }  
  
        DateTime DateJoined { get; }  
        DateTime? LastOrder { get; }  
        string Name { get; }  
        IDictionary<DateTime, string> Reminders { get; }  
    }  
    // </SnippetICustomerVersion1>  
}  

IOrder.cs

using System;

namespace customer_relationship { // <SnippetIOrderVersion1> public interface IOrder { DateTime Purchased { get; } decimal Cost { get; } } // </SnippetIOrderVersion1> }

**SampleCustomer.cs**

using System; using System.Collections.Generic;

namespace customer_relationship { public class SampleCustomer : ICustomer { public SampleCustomer(string name, DateTime dateJoined) => (Name, DateJoined) = (name, dateJoined);

    private readonly List<IOrder> allOrders = new List<IOrder>();  

    public IEnumerable<IOrder> PreviousOrders => allOrders;  

    public DateTime DateJoined { get; }  

    public DateTime? LastOrder { get; private set; }  

    public string Name { get; }  

    private readonly Dictionary<DateTime, string> reminders = new Dictionary<DateTime, string>();  
    public IDictionary<DateTime, string> Reminders => reminders;  

    public void AddOrder(IOrder order)  
    {  
        if (order.Purchased > (LastOrder ?? DateTime.MinValue))  
            LastOrder = order.Purchased;  
        allOrders.Add(order);  
    }  
}  
}

Program.cs

using System;

namespace customer_relationship { // <SnippetIOrderVersion1> public interface IOrder { DateTime Purchased { get; } decimal Cost { get; } } // </SnippetIOrderVersion1> }

Note:此问题总结整理于: Where to put the code?

C#
C#
一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。
140 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 44,186 信誉分 Microsoft 供应商
    2024-04-09T08:19:04.07+00:00

    文档指出“您可以在 GitHub 上的示例存储库中看到整个完成的代码。 我们可以看到,“public static void SetLoyaltyThresholds SetLoyaltyThresholds”代码被放在ICustomer.cs中。


    如果回复有帮助,请点击“接受答案”并点赞。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助