TheBulgarianWolf

Songs(Class and Objects Example)

Jan 2nd, 2021
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Songs
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("Enter the number of lines you're about to enter: ");
  12.             int count = int.Parse(Console.ReadLine());
  13.             List<Song> songs = new List<Song>();
  14.             for(int i = 0; i < count; i++)
  15.             {
  16.                 string[] input = Console.ReadLine().Split("_");
  17.                 string typeList = input[0];
  18.                 string name = input[1];
  19.                 string time = input[2];
  20.  
  21.                 Song song = new Song();
  22.                 song.TypeList = typeList;
  23.                 song.Name = name;
  24.                 song.Time = time;
  25.                 songs.Add(song);
  26.             }
  27.  
  28.             string targetListType = Console.ReadLine();
  29.            
  30.             //1st example
  31.             foreach(Song currentSong in songs)
  32.             {
  33.                 if(currentSong.TypeList == targetListType)
  34.                 {
  35.                     Console.WriteLine(currentSong.Name);
  36.                 }
  37.             }
  38.  
  39.             //2nd example
  40.             songs = songs.FindAll(x => x.TypeList == targetListType);
  41.             foreach (Song currentSong in songs)
  42.             {
  43.                 Console.WriteLine(currentSong.Name);
  44.             }
  45.  
  46.             //3rd example
  47.             songs.Where(x => x.TypeList == targetListType).ToList().ForEach(x => Console.WriteLine(x.Name));
  48.  
  49.  
  50.         }
  51.     }
  52.  
  53.     class Song
  54.     {
  55.         public string TypeList { get; set; }
  56.         public string Name { get; set; }
  57.         public string Time { get; set; }
  58.     }
  59. }
  60.  
Add Comment
Please, Sign In to add comment