Home > Software engineering >  Passing Argument in C#
Passing Argument in C#

Time:12-19

How can I input "English" as a default language, if the user doesn't input their preferred language? Without using if/else statement.

Here is my simple code:

using System;

namespace Practice1._3
{
    class Program
    {            
        static void Main(string[] args)
        {
            Console.WriteLine("What's your name:");
            string name = Console.ReadLine();
            Console.WriteLine("Age:");
            string Age = Console.ReadLine();
            Console.WriteLine("Preferred language:");
            string language = Console.ReadLine();


            Console.WriteLine("\n-----------------------------------------");
            Console.WriteLine("Your name is "   name);           
            Console.WriteLine("Your age is "   Age);
            Console.WriteLine("Your language is "   language);

        }
    }
}

CodePudding user response:

You could use a ternary conditional operator.

Console.WriteLine("Your language is : {0}", language == "" ? "English" : language);

CodePudding user response:

If you can't use if/else statements or ternary operators, you can use the following approach to input "English" as a default language if the user doesn't input their preferred language:

Declare a default language variable and initialize it to "English": string defaultLanguage = "English";

Read the user's preferred language: string language = Console.ReadLine();

Use the null-coalescing operator (??) to assign the default language to the language variable if the user's input is null or an empty string: language = language ?? defaultLanguage;

The null-coalescing operator (??) returns the left-hand operand if it is not null, and the right-hand operand otherwise. In this case, if the user's input is not null or an empty string, the language variable is assigned the value of the user's input. Otherwise, it is assigned the default language.

Here is the modified code:

using System;
  
namespace Practice1._3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("What's your name:");
            string name = Console.ReadLine();
            Console.WriteLine("Age:");
            string Age = Console.ReadLine();
            Console.WriteLine("Preferred language:");
            string language = Console.ReadLine();
            string defaultLanguage = "English";
            language = language ?? defaultLanguage;

            Console.WriteLine("\n-----------------------------------------");
            Console.WriteLine("Your name is "   name);
            Console.WriteLine("Your age is "   Age);
            Console.WriteLine("Your language is "   language);

        }
    }
}

CodePudding user response:

Because of this line string language = Console.ReadLine();, it cannot be possible to do it without checking what is already stored in the variable. I was thinking of a default value. Even if you set a default value, this line will override it with an empty string. Check here for more about string vs null

Like A. Petit suggests above, I also suggest an if statement to check the input and display your language accordingly. You can use ternary for this. a ternary Is also a form of the if statement.

Console.WriteLine("What's your name:");
string name = Console.ReadLine();
Console.WriteLine("Age:");
string Age = Console.ReadLine();
Console.WriteLine("Preferred language:");
string language = Console.ReadLine();

//check if language is entered. If not, use a default langage
language = language=="" || language == null? "English": language;

Console.WriteLine("\n-----------------------------------------");
Console.WriteLine("Your name is "   name);
Console.WriteLine("Your age is "   Age);
Console.WriteLine("Your language is "   language);
  •  Tags:  
  • c#
  • Related