1

I'm new to programming
i started to learn C# language

        Console.WriteLine("yes or no ?");
        string answer = Console.ReadLine();
        switch (answer)
        { 
            case "yes":
                // some code 
                break;
            case "no":
                //some code 
                break;
            default:
                //some code 
                break;

my question is if the user inserted his answer "Yes", "YES" ,"yEs" or whatever the way he wrote "yes" the program will execute the default code because its not exactly matching to the lowercase "yes" i wrote in the switch case .. is there an advanced way to let the program detect that "Yes" with the uppercase letter is the the same answer to "yes" and then execute the block code of case "yes".

my 2nd question i picked C# to be my first language because Unity3d game engine support this language so i decided to learn the language at first then use it inside the unity3d is that a right decision

im sorry if my question seems stupid but as i said im newbie

3
  • ToLower extension method. :) somesting.ToLower () Commented Dec 17, 2016 at 16:33
  • Your second question regarding which language to use is off-topic because it's subjective and completely opinion-based. Commented Dec 17, 2016 at 16:36
  • @TheNoob ToLower() is a normal String method, not an extension method. Commented Dec 18, 2016 at 3:36

3 Answers 3

5

Convert the user's input to lowercase using String.ToLower() before comparing it against your (lowercase) choices. You should also consider removing leading/trailing whitespace from the string as well, using String.Trim():

    switch (answer.Trim().ToLower())
    { 
        case "yes":
            // some code 
            break;
        case "no":
            //some code 
            break;
        default:
            //some code 
            break;
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the following:

switch(answer.ToLower())

This will convert all the characters to lowercase.

Regarding the Unity 3D, I think C# is really very flexible and extensive language and it will let you do many things. It keeps getting better with every release and you have vast collections of namespaces and functions to help you through it.

For learning things related to Unity 3D Microsoft also provides video tutorials for the beginner to high level at https://mva.microsoft.com/training-topics/game-development#!jobf=Developer&lang=1033

At this link you can find bunch of video series by Microsoft which will get you started.

Comments

0

You have to convert his answer to upper or lower case so it'll be recognized regardless.

switch (answer.ToUpper())
{ 
    case "YES":
        // some code 
        break;
    case "NO":
        //some code 
        break;
    default:
        //some code 
        break;

OR

switch (answer.ToLower())
{ 
    case "yes":
        // some code 
        break;
    case "no":
        //some code 
        break;
    default:
        //some code 
        break;

And as for picking C# as your first langauge, it's completely fine if you're looking to make games in Unity. If you wish to try GameMaker, learning Java will do good for you.

I don't agree with all the people saying that Python should be your first language. It all depends on what you wanna code and for your cause, C# is very good.

2 Comments

First example will always hit default
This answer effectively duplicates mine. It's also incorrect.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.