Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ResearchNotes.Samples.Misc.CodeForOthers
- {
- internal class NameLooper
- {
- private static bool m_RepeatProcess = false;
- public static void Main()
- {
- /// If you'd like this to persist, you can create a static class member (not in a method)
- /// Though, it will exit after it's finished. I'll add some logic to demonstrate how
- /// we would make this cycle repeat which will hopefully give you an idea of why
- /// we encapsulate small functions in methods instead of just writing it all in this Main block.
- string[] names = new string[5];
- CollectNames(names);
- OutputNames(names);
- DisplayExitOutput();
- }
- private static void CollectNames(string[] names)
- { /// Notice here we do not use a hard-coded '5' but
- /// rather the length of the collection so we don't
- /// have to change it in multiple places if we need to.
- /// (DRY) Don't Repeat Yourself (ETC) Easy To Change
- Console.WriteLine($"Enter ({names.Length}) Names");
- for(int i = 0; i < names.Length; i++)
- {
- /// You could simply assign this directly to the collection
- /// But we've captured it here so we don't have to reach
- /// into the the array to print it out afterwards.
- var name = Console.ReadLine();
- names[i] = name;
- Console.WriteLine($"Name [{i}]: {name}");
- }
- }
- private static void OutputNames(string[] names)
- {
- Console.WriteLine($"Here's a list of ({names.Length})!");
- for(int i = 0; i < names.Length; i++)
- {
- Console.WriteLine($"Name [{i}]: {names[i]}");
- }
- }
- private static void DisplayExitOutput()
- {
- Console.WriteLine("Press Any Key To Exit!");
- Console.ReadLine();
- }
- /// Want it to repeat the process? Simple!
- /// <summary>
- /// Just for demonstration purposes. By using this you would not
- /// have to pass the array to every method. The choice to do one
- /// or the other is circumstancial, you'll learn that as you continue.
- /// </summary>
- private static string[] m_NameCollection = new string[5];
- private static void BeginNamingProcess(string[] names)
- {
- CollectNames(names);
- OutputNames(names);
- /// Passing the names via parameter will allow these to persist
- EndNamingProcess(names, m_RepeatProcess);
- }
- private static void EndNamingProcess(string[] names, bool repeat)
- {
- if(repeat)
- {
- BeginNamingProcess(names);
- }
- else
- {
- DisplayExitOutput();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement