C# User Input
Getting User Input
● We have already learned that Console.WriteLine() is used to output
(print) values. Now we will use Console.ReadLine() to get user input. ●
In the following example, the user can input his or her username, which
is stored in the variable userName. Then we print the value of
userName:
Example
// Type your username and press enter
Console.WriteLine("Enter username:");
/* Create a string variable and get user input from the keyboard and
store it in the variable */
string userName = Console.ReadLine();
/* Print the value of the variable (userName),which will display the
input value */
Console.WriteLine("Username is: " + userName);
User Input and Numbers
The Console.ReadLine() method returns a string. Therefore, you cannot get
information from another data type, such as int. The following program will cause an
error:
Example
Console.WriteLine("Enter your age:");
int age = Console.ReadLine();
Console.WriteLine("Your age is: " + age);
● The error message will be something like this:
Cannot implicitly convert type 'string' to 'int'
● Like the error message says, you cannot implicitly convert type 'string' to 'int'.
● However, the above issue can be easily solved by converting any
type explicitly, by using one of theConvert.To methods:
Example
Console.WriteLine("Enter your age:");
int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your age is: " + age);
Note: If you enter wrong input (e.g. text in a numerical input), you will get an
exception/error message (like System.FormatException: 'Input string was not in a
correct format.')
C# - Type Conversion
- Type conversion is converting one type of data to anothertype. ∙ It is also known as
Type Casting. In C#, type casting has two forms:
- Implicit type conversion: These conversions are performed by C# in a type-safe
manner. For example, conversions from smaller to larger integral types. - Explicit
type conversion: These conversions are done explicitly by users using the
pre-defined functions. Explicit conversions require a cast operator - (i.e
Convert.ToInt32(), (int) ).
- The following example shows an explicit type conversion.
double d = 5673.74;
int i, j;
// cast double variable ‘d’ to int.
i = (int) d;
j = Convert.ToInt32(d);
Console.WriteLine(i);
Console.WriteLine(j);
When the above code is compiled and executed, it produces the following result:
5673
5673
The following example shows an implicit type conversion
int myInt = 9;
double myDouble = myInt
Console.WriteLine(myInt); // Outputs 9
Console.WriteLine(myDouble); // Outputs 9.0 or just 9 in console
C# Type Conversion Methods
C# provides the following built-in type conversion methods:
Methods & Description
ToBoolean()
Converts a type to a Boolean value, where possible.
ToByte()
Converts a type to a byte.
ToChar()
Converts a type to a single Unicode character, where
possible.
ToDateTime()
Converts a type (integer or string type) to date-time
structures.
ToDecimal()
Converts a floating point or integer type to a decimal type.
ToDouble()
Converts a type to a double type.
ToInt16()
Converts a type to a 16-bit integer.
ToInt32()
Converts a type to a 32-bit integer.
ToInt64()
Converts a type to a 64-bit integer.
ToSbyte()
Converts a type to a signed byte type.
ToString()
Converts a type to a string.
ToUInt16()
Converts a type to an unsigned int type.
ToUInt32()
Converts a type to an unsigned long type.
ToUInt64()
Converts a type to an unsigned big integer.
Example: The following example tests type conversion using various C# type
conversion methods:
int myInt = 10;
double myDouble = 5.25;
bool myBool = true;
Console.WriteLine(Convert.ToString(myInt)); // convert int to string
Console.WriteLine(Convert.ToDouble(myInt)); // convert int to double
Console.WriteLine(Convert.ToInt32(myDouble)); // convert double to int
Console.WriteLine(Convert.ToString(myBool)); // convert bool to string When the
above code is compiled and executed, it produces the following result:
10
10
True
C# String Interpolation
- String interpolation is a way to insert variables directly inside a string. - In
C#, we use the $ symbol before the string and wrap variables in { }. - It
makes code cleaner and easier to read than using + for concatenation.
Syntax:
string name = "Alice";
int age = 20;
Console.WriteLine($"Hello, my name is {name} and I am {age} years
old."); Output:
Hello, my name is Alice and I am 20 years old.