I am trying to teach myself C#. I recently completed this exercise. The code works the the way its suppose to. My question is how could I make this more efficient or is there bad programing practices? Thanks

using System;
class testcontact
{
    public static void Main()
{
    InputWrapper iw = new InputWrapper();
    string cmd;
    
    do
    {
        
         Console.WriteLine("Add");   // menu of options
         Console.WriteLine("Remove");
         Console.WriteLine("Forward");
         Console.WriteLine("Reverse");
         Console.WriteLine("Find");
         Console.WriteLine("Quit");
         
        cmd = iw.getString(">");    // user input from Input wrapper class
        
     
        
    switch (cmd)
    { 

    case ("add"):    // new contact
    Console.WriteLine("\nAdding new contact\n");
    break;
    
    case "remove":  // remove contact
    Console.WriteLine("\nRemoving contact\n");
    break;

    case "forward":   //sort forward
    Console.WriteLine("\nSorting contacts forward\n");
    break;

    case "reverse":   // reverse sort
    Console.WriteLine("\nRevers contact sort\n");
    break;

    case "find":   // find contact
    Console.WriteLine("\nFind contact\n");
    break;

    default:    // all invalid commands will go here
    Console.WriteLine("valid commands are \n add \n remove \n forward \n reverse \n find quit\n");
    break;
    }
    
    }
    while (cmd != "quit");

    }

}

Edited 6 Years Ago by bornwith: spelling

I am trying to teach myself C#. I recently completed this exercise. The code works the the way its suppose to. My question is how could I make this more efficient or is there bad programing practices? Thanks

using System;
class testcontact
{
    public static void Main()
{
    InputWrapper iw = new InputWrapper();
    string cmd;
    
    do
    {
        
         Console.WriteLine("Add");   // menu of options
         Console.WriteLine("Remove");
         Console.WriteLine("Forward");
         Console.WriteLine("Reverse");
         Console.WriteLine("Find");
         Console.WriteLine("Quit");
         
        cmd = iw.getString(">");    // user input from Input wrapper class
        
     
        
    switch (cmd)
    { 

    case ("add"):    // new contact
    Console.WriteLine("\nAdding new contact\n");
    break;
    
    case "remove":  // remove contact
    Console.WriteLine("\nRemoving contact\n");
    break;

    case "forward":   //sort forward
    Console.WriteLine("\nSorting contacts forward\n");
    break;

    case "reverse":   // reverse sort
    Console.WriteLine("\nRevers contact sort\n");
    break;

    case "find":   // find contact
    Console.WriteLine("\nFind contact\n");
    break;

    default:    // all invalid commands will go here
    Console.WriteLine("valid commands are \n add \n remove \n forward \n reverse \n find quit\n");
    break;
    }
    
    }
    while (cmd != "quit");

    }

}

Given a first look, the only change I could suggest would be using an Enum rather than hard coding your values in the switch. an Enum is a collection of constant string values, that are usable in the cases of a switch statement. this way, you can refer to it with the Intellisense too.

The other thing I maybe would change would be using a Console.ReadLine() rather than an inputWrapper but that's cause it's the way I always worked so I don't know much about InputWrapper. But the Enum is the thing I'd consider the most in the suggestions

Edited 6 Years Ago by GuillaumeCantin: n/a

//EchoName.cs
using System;
class Echo
{
    public static void Main(string[] args)
    {
        InputWrapper iw = new InputWrapper();
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.