【问题标题】:IEnumerable<IAppointment> is causing 11 errors at class Appointments.csIEnumerable<IAppointment> 导致 Appointments.cs 类出现 11 个错误
【发布时间】:2015-03-29 15:48:05
【问题描述】:

错误:

'Calendar.Appointments' 没有实现接口成员'System.Collections.Generic.ICollection.CopyTo(Calendar.IAppointment[], int)'

add、clear、count、IsReanOnly、Remove、Indexof 等也一样...

定义是:

  • 它必须被称为 Appointments 并且它必须有一个空的构造函数。
  • 必须实现接口IAppointments

接口IAppointments如下:

public interface IAppointments : IList<IAppointment> 
{ 
    bool Load();
    bool Save();
    IEnumerable<IAppointment> GetAppointmentsOnDate(DateTime date);
}

界面IAppointment如下:

public interface IAppointment 
{ 
   DateTime Start { get; }
   int Length { get; }
   string DisplayableDescription { get; }
   bool OccursOnDate(DateTime date); 
}

因为实现IAppointments的类也必须实现IList&lt;IAppointment&gt;(根据上面接口的定义),您可能会考虑将Appointments类实现为实现IList&lt;IAppointment&gt;的类的子类- 如List&lt;IAppointment&gt;

我写道:

 public class Appointments : IAppointments
 {
    List<IAppointment> list = null;

    public bool Load() 
    {
        bool flag = false;
        GetEnumerableList(list);
        return flag;
    }

    public bool Save() 
    {
        bool flag = false;

        return flag;
    }

    public IEnumerable<IAppointment> GetAppointmentsOnDate(DateTime day) 
    {
        List<IAppointment> day_list = new List<IAppointment>();

        foreach (var item in list)
        {
            if (item.Start == day) day_list.Add(item);
        }

        return day_list;
    }

    public List<IAppointment>    
    GetEnumerableList(IEnumerable<IAppointment> Enumerable)
    {
        foreach (var item in Enumerable)
        {
            list.Add(item);
        }
        return list;
    }

  public class Appointment : IEnumerable<IAppointment>, IAppointment
  {
      public DateTime Start { get; set; }
      public int Length { get; set; }
      public string DisplayableDescription { get; set; }

      public bool OccursOnDate(DateTime date)
      {
        return false;
      }

      IEnumerator<IAppointment> IEnumerable<IAppointment>.GetEnumerator()
      {
          throw new NotImplementedException();
      }

      System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
      {
          throw new NotImplementedException();
      }
  }

但没有运气,我很困惑。请您帮我如何在AppoinmentsAppointment 中实现IEnumerated&lt;IAppointment&gt; 并继承它?我相信我没有用我的AppointmentAppointments 实现并且没有正确继承IAppointmentIAppointments

【问题讨论】:

  • 从外观上看,我认为您不需要分别从IList&lt;IAppointment&gt;IEnumerable&lt;IAppointment&gt; 继承IAppointments Appointment。否则,请说明您的要求。
  • 谢谢bsarkar!我离开公共类 Appointment : IAppointment 和公共类 Appointments : IAppointments 而不继承 IEnumerable 但我仍然收到我在消息中提到的错误。
  • @Konstantinos 通常,显示问题所需的minimal code example 应包含在问题本身中。任何指向其他地方包含的完整代码的链接都可以被认为是有用的,但它们仍然是次要的,只是用来提供一些额外的上下文。因此,如果您更改了某些内容,但仍然无法正常工作,请edit 您的问题,或者在花费一些时间搜索和评估后,如果问题根本不同,请发布新问题。
  • 请不要使用指向外部资源的链接,无论是页面、图像等,以支持您的问题。外部链接可以直接更改或删除,使您的问题毫无用处。请不要使用 cmets 提供您应该在问题中提供的信息。请确保您的问题完全独立,并提供a good, minimal, complete code example,清楚地说明您的问题。
  • 我只是希望有人向我解释。我有 Apointments 课程。在加载函数中,我想从文件中将所有数据导入到 IEnumerable 列表中,如果它发生在确切的日期。错误是 'Calendar.Appointments' 没有实现接口成员 'System.Collections.Generic.ICollection.CopyTo(Calendar.IAppointment[], int)' 对于 add、clear、count、IsReanOnly、Remove、Indexof 等也是相同的。 .. System.Collections.Generic.ICollection。如何在 Visual Studio 2013 中克服 C# 中的这个错误?我试过了,但没有运气。

标签: c# inheritance collections ienumerable abstract


【解决方案1】:

将 Appointments 类声明为

public class Appointments : List<IAppointment>, IAppointments

并删除

List<IAppointment> list = null;

在您当前的实现中将 list 更改为 this

【讨论】:

    猜你喜欢
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    相关资源
    最近更新 更多