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.

  • And in what class, I'm sorry, do you implement IEnumerable ? The presence of the public IEnumerable Search(string name) method public IEnumerable Search(string name) not an implementation of this interface. - Bulson
  • @Bulson, sorry. I acted on the example found on the Internet. On assignment create an iterator. It seems simple, but again the problems, as they were with IComparable. - Valera
  • Thank you read - Valera
  • @Bulson, yes, I realized that I did not, but still how to describe these methods - Valera

1 answer 1

I did it

 public class SoftwareCollection : ICollection<ASoftware> { private List<ASoftware> _softwares = new List<ASoftware>(); #region РСализация ICollection<T> public int Count => _softwares.Count; public bool IsReadOnly => false; public void Add(ASoftware item) { if (item == null) throw new ArgumentNullException(nameof(item)); _softwares.Add(item); } public void Clear() { _softwares.Clear(); } public bool Contains(ASoftware item) { if (item == null) throw new ArgumentNullException(nameof(item)); return _softwares.Contains(item); } public void CopyTo(ASoftware[] array, int arrayIndex) { if (array == null) throw new ArgumentNullException(nameof(array)); _softwares.CopyTo(array, arrayIndex); } public IEnumerator<ASoftware> GetEnumerator() { return _softwares.GetEnumerator(); } public bool Remove(ASoftware item) { if (item == null) throw new ArgumentNullException(nameof(item)); return _softwares.Remove(item); } IEnumerator IEnumerable.GetEnumerator() { return _softwares.GetEnumerator(); } #endregion /// <summary> /// Π‘ΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²ΠΊΠ° /// </summary> public void Sort() { _softwares.Sort(); } /// <summary> /// ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ Π½ΡƒΠΆΠ½ΠΎΠ³ΠΎ Ρ‚ΠΈΠΏΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌ /// </summary> /// <typeparam name="T">Ρ‚ΠΈΠΏ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌ Π² ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ</typeparam> /// <param name="type">искомый Ρ‚ΠΈΠΏ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌ</param> /// <returns>ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ Π½ΡƒΠΆΠ½ΠΎΠ³ΠΎ Ρ‚ΠΈΠΏΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌ</returns> public List<T> GetSoftwarByType<T>(Type type) where T : class { return _softwares.Where(s => s.GetType().Equals(type)) .Select(s => s as T) .ToList(); } } 

We work with it like this

 class Program { static void Main(string[] args) { SoftwareCollection softwares = new SoftwareCollection { new FreeSoftware("FileZilla", "БША"), new ProprietarySoftware("Adobe Photoshop", "БША", DateTime.Parse("12.12.2018"), TimeSpan.FromDays(360), 3600), new SharewareSoftware("WinRar", "Россия", DateTime.Parse("12.12.2018"), TimeSpan.FromDays(90)), new ProprietarySoftware("ABBYY FineReader", "Россия", DateTime.Parse("06.06.2017"), TimeSpan.FromDays(360), 2800), new FreeSoftware("FireFox Mozilla", "БША") }; // Console.WriteLine("=====Π”ΠΎ сортировки======"); PrintCollection(softwares); Console.WriteLine("========================"); Console.WriteLine("Для продолТСния Π½Π°ΠΆΠΌΠΈΡ‚Π΅ Π’Π²ΠΎΠ΄..."); Console.ReadLine(); softwares.Sort(); Console.WriteLine("=====ПослС сортировки======"); PrintCollection(softwares); Console.WriteLine("========================"); Console.WriteLine("Для продолТСния Π½Π°ΠΆΠΌΠΈΡ‚Π΅ Π’Π²ΠΎΠ΄..."); Console.ReadLine(); var freeSoftwares = softwares.GetSoftwarByType<FreeSoftware>(typeof(FreeSoftware)); Console.WriteLine("=====Волько Free====="); foreach (var item in freeSoftwares) { Console.WriteLine(item); } Console.WriteLine("========================"); Console.ReadKey(); } private static void PrintCollection(SoftwareCollection softwares) { foreach (var software in softwares) { if (software is FreeSoftware) { Console.WriteLine((software as FreeSoftware)); } else if (software is SharewareSoftware) { Console.WriteLine((software as SharewareSoftware)); } else if (software is ProprietarySoftware) { Console.WriteLine((software as ProprietarySoftware)); } else { Console.WriteLine(software); } Console.WriteLine(); } } } 

An example can be found here . I recommend to see, because there you can still IComparable<T> implementation of IComparable<T> .

  • Vuh, unexpected answer) thanks a lot for the help !!! Be sure to watch! - Valera
  • yes a great example, thank you very much, very good implementation But how to implement an iterator to search for elements of the collection that satisfy a given condition. - Valera
  • @Valera what is the given condition? You wanted a method that would return a collection of the necessary types, I implemented it for you. Than you are not satisfied with the implementation of various conditions using LINQ ? - Bulson
  • this is the problem that suits me.) I did it, but the accepting job said to do it through an iterator. Let's say to bring by coincidence of the name - Valera
  • I get it, I'm thinking now ... - Bulson