Advertisement
Guest User

Untitled

a guest
Jan 19th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | Software | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ResearchNotes.Samples.Misc.CodeForOthers
  8. {
  9.     internal class NameLooper
  10.     {  
  11.         private static bool m_RepeatProcess = false;
  12.         public static void Main()
  13.         {
  14.             /// If you'd like this to persist, you can create a static class member (not in a method)
  15.             /// Though, it will exit after it's finished. I'll add some logic to demonstrate how
  16.             /// we would make this cycle repeat which will hopefully give you an idea of why
  17.             /// we encapsulate small functions in methods instead of just writing it all in this Main block.
  18.             string[] names = new string[5];
  19.  
  20.             CollectNames(names);
  21.  
  22.             OutputNames(names);
  23.  
  24.             DisplayExitOutput();
  25.         }
  26.  
  27.         private static void CollectNames(string[] names)
  28.         {   /// Notice here we do not use a hard-coded '5' but
  29.             /// rather the length of the collection so we don't
  30.             /// have to change it in multiple places if we need to.
  31.             /// (DRY) Don't Repeat Yourself (ETC) Easy To Change
  32.             Console.WriteLine($"Enter ({names.Length}) Names");
  33.             for(int i = 0; i < names.Length; i++)
  34.             {
  35.                 /// You could simply assign this directly to the collection
  36.                 /// But we've captured it here so we don't have to reach
  37.                 /// into the the array to print it out afterwards.
  38.                 var name = Console.ReadLine();
  39.  
  40.                 names[i] = name;
  41.  
  42.                 Console.WriteLine($"Name [{i}]: {name}");
  43.             }
  44.         }
  45.  
  46.         private static void OutputNames(string[] names)
  47.         {
  48.             Console.WriteLine($"Here's a list of ({names.Length})!");
  49.             for(int i = 0; i < names.Length; i++)
  50.             {
  51.                 Console.WriteLine($"Name [{i}]: {names[i]}");
  52.             }
  53.         }
  54.  
  55.         private static void DisplayExitOutput()
  56.         {
  57.             Console.WriteLine("Press Any Key To Exit!");
  58.             Console.ReadLine();
  59.         }
  60.  
  61.         /// Want it to repeat the process? Simple!  
  62.         /// <summary>
  63.         /// Just for demonstration purposes. By using this you would not
  64.         /// have to pass the array to every method. The choice to do one
  65.         /// or the other is circumstancial, you'll learn that as you continue.
  66.         /// </summary>
  67.         private static string[] m_NameCollection = new string[5];
  68.         private static void BeginNamingProcess(string[] names)
  69.         {
  70.             CollectNames(names);
  71.             OutputNames(names);
  72.  
  73.             /// Passing the names via parameter will allow these to persist
  74.             EndNamingProcess(names, m_RepeatProcess);
  75.         }
  76.  
  77.         private static void EndNamingProcess(string[] names, bool repeat)
  78.         {
  79.             if(repeat)
  80.             {
  81.                 BeginNamingProcess(names);
  82.             }
  83.  
  84.             else
  85.             {
  86.                 DisplayExitOutput();
  87.             }
  88.         }
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement