The time has come to work with interfaces and a problem has come. I do not understand how it works under a certain condition, an Iterator for finding elements of a collection that satisfy a given condition. Here is my code and attempt to implement IEnumerable:
class Program { static void Main(string[] args) { List<Software> soft = new List<Software>() {new FreeSoftware("Adobe", "China"), new FreeSoftware("Illustration", "China"), new FreeSoftware("Vdobe", "China"), new FreeSoftware("Kaspersky", "China"), new SharewareSoftware("Dr.Web", "USA", new DateTime(2019, 03, 04), 0, 0, 6), new ProprietarySoftware("Avast", "Australia", new DateTime(2019, 02, 04), 0, 0, 6, 200) }; Collection c = new Collection(soft); c.Search("Vdobe"); Console.ReadKey(); } } public abstract class Software : IComparable<Software> { public string name_soft; public string made; public virtual bool can => true; public int CompareTo(Software soft) { Software p = soft as Software; if (p == null) throw new InvalidCastException(); if (name_soft.First() > p.name_soft.First()) return 1; else if (name_soft.First() == p.name_soft.First()) return 0; else return -1; } public override string ToString()=>$"ΠΠ°Π·Π²Π°Π½ΠΈΠ΅ ΠΏΡΠΎΠ΄ΡΠΊΡΠ° - {name_soft} Π‘ΡΡΠ°Π½Π° ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ΄ΡΡΠ²Π° - {made}"; public void Search(List<Software> soft) { Console.WriteLine("\n\n\n\n ~ΠΠΎΠΈΡΠΊ ΡΠΎΡΡΠ° ΠΊΠΎΡΠΎΡΡΠΉ ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ~"); foreach (var i in soft) { if (i.can) { Console.WriteLine($"{i.ToString()}"); } } } } public class FreeSoftware : Software { public FreeSoftware(string name, string made) { base.name_soft = name; base.made = made; } public override string ToString() { return base.ToString(); } public override bool can => base.can; } public class SharewareSoftware : Software { public DateTime date_instal; private int year, month, day; public DateTime period_free; public SharewareSoftware(string name, string made, DateTime date_instal, int year, int month, int day) { base.name_soft = name; base.made = made; this.date_instal = date_instal; this.year = year; this.month = month; this.day = day; period_free = date_instal.AddDays(day).AddMonths(month).AddYears(year); } public override bool can => DateTime.Now < period_free; public override string ToString()=>$"{base.ToString()} ΠΠ°ΡΠ° ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠΈ {date_instal.ToString("d")} ΠΠ΅ΡΠΈΠΎΠ΄ Π±Π΅ΡΠΏΠ»Π°ΡΠ½ΠΎΠ³ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ Π΄ΠΎ {period_free.ToString("d")}"; } public class ProprietarySoftware : Software { public DateTime date_instal; public DateTime time_use; private int year, month, day; private double price; public ProprietarySoftware(string name, string made, DateTime date_instal, int year, int month, int day, double price) { base.name_soft = name; base.made = made; this.date_instal = date_instal; this.year = year; this.month = month; this.day = day; time_use = date_instal.AddDays(day).AddMonths(month).AddYears(year); this.price = price; } public override bool can => DateTime.Now < time_use; public override string ToString()=> $"{base.ToString()} ΠΠ°ΡΠ° ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠΈ {date_instal.ToString("d")} ΠΡΠ΅ΠΌΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ Π΄ΠΎ {time_use.ToString("d")} Π¦Π΅Π½Π° {price}"; } public class Collection : Software,IEnumerable<Software> { private List<Software> soft; public Collection() : this(new List<Software>()) { } public Collection(List<Software> soft) { this.soft = soft; } public void Add_To_Collection(Software s)=>this.soft.Add(s); public void Remove_To_Collection(int a) =>this.soft.RemoveAt(a); public void Show_Collection() { foreach (var i in soft) { Console.WriteLine(i.ToString()); } } public override string ToString()=> $"ΠΠ°Π·Π²Π°Π½ΠΈΠ΅ ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ {nameof(soft)} ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΡ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ {soft.Count} ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ²"; public void Sort()=> soft.Sort(); public List<Software> Soft { get => soft; } public IEnumerable Search(string name) { foreach (var i in soft) { if (i.name_soft == name) { yield return i.ToString(); } } } public IEnumerator<Software> GetEnumerator() { return ((IEnumerable<Software>)soft).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable<Software>)soft).GetEnumerator(); } }
I tried to implement, but something I did wrong and does not work ... I understand that I do not understand correctly how to implement .. And hereβs another question that would not create another topic: create a generic method with type return List, which returns from the collection only those objects whose type is specified as a generalized parameter of the method, that is, the type of the child class is specified as the generalized parameter of the method, and only objects of the specified type are returned from the collection.
IEnumerable
? The presence of thepublic IEnumerable Search(string name)
methodpublic IEnumerable Search(string name)
not an implementation of this interface. - Bulson