Input and Output are preferably the most important statements in a programming language.
So far, we have seen the Output command of C#(i.e. System.Console.WriteLine(...)).
public class MyApplication { public static void Main(string[] args) { System.Console.WriteLine("Hello World"); } }
And we are yet to look at the Input statement.
So, let us look at the Input statement. Then we will be exploring a few more ways to use the Output statement.
So far, we have used variables to store values. But the variables were loaded with values.
public class MyApplication { public static void Main(string[] args) { int x = 9; System.Console.WriteLine("The value of x is : "+x); } }
But, what if you don't want the variable x to contain a fixed value like 9. Rather you want the user to enter any value of their choice.
And this is where the Input statements comes to picture.
Just like the Output statement is called System.Console.WriteLine(). Same way the Input statement is called as System.Console.ReadLine().
Let us understand ReadLine() statement with the below example.
public class MyApplication { public static void Main(string[] args) { System.Console.Write("Enter any text : "); string x = System.Console.ReadLine(); System.Console.WriteLine("The text you have entered is : "+x); } }
Now, if you see the Output. The first line says,
Enter any text : Hello
Now, let us go to the above code and see what happened?
In the above code, we have used the print statement,
System.Console.Write("Enter any text : ")
Then we have declared a variable x, which is initially empty.
string x = System.Console.ReadLine();
So, what happened is, the below String is Printed,
Enter any text :
Then we have the statement,
string x = System.Console.ReadLine();
ReadLine() is used to get an input from the user, and if the user enters a value, it goes and sits inside variable x.
So, the screen is stuck, until user types any number.
In the above case, I have typed Hello.
Enter any text : Hello
And Hello goes and sits inside x.
And we got the output,
The text you have entered is : Hello
So, we have seen how can we get a String/Text as an input.
Now, let us say, you want to get a number as an input.
In that case, we can also use System.Console.ReadLine(), but with a minimal change.
public class MyApplication { public static void Main(string[] args) { System.Console.Write("Enter a number of your choice : "); string x = System.Console.ReadLine(); int num = System.Convert.ToInt32(x); System.Console.WriteLine("The number you have entered is : "+num); } }
Now, if you see the Output. The first line says,
Enter any number of your choice : 21
Now, let us go to the above code and see what happened?
In the above code, we have used the System.Console.WriteLine statement,
System.Console.Write("Enter a number of your choice : ")
Then we have declared a variable x, which is initially empty.
string x = System.Console.ReadLine();
So, what happened is, the below String is Printed,
Enter a number of your choice :
Then we have the statement,
string x = System.Console.ReadLine();
ReadLine() is used to get an input from the user, and if the user enters a value, it goes and sits inside variable x.
So, the screen is stuck, until user types any number.
In the above case, I have typed 21.
Enter any text : 21
And 21 goes and sits inside x.
Now, there is the actual catch.
The number 21 is treated as a string. Because 21 is assigned to a string type variable x.
string x = System.Console.ReadLine();
But we want 21 as a number.
And that is why we have used the converter,
int num = System.Convert.ToInt32(x);
To convert the value of x (i.e. 21 as string) to integer.
And the new value is assigned to int type variable num.
And we got the output,
The text you have entered is : Hello
Next, let us look at C# Output.
A simple example of System.Console.WriteLine(...) statement would be,
public class MyApplication { public static void Main(string[] args) { int x = 9; System.Console.WriteLine("The value of x is : "+x); } }
With the System.Console.WriteLine(...) statement, C# supports something called as String Interpolation.
Let us dive into an example to understand String Interpolation.
public class MyApplication { public static void Main(string[] args) { int x = 5; int y = 8; int z = x + y; System.Console.WriteLine($"The added result of {x} and {y} is {z}"); } }
So, in the above example, we have three variables, x, y and z.
Now, inside the System.Console.WriteLine(...) statement, we have used x, y and z inside ${}.
System.Console.WriteLine($"The added result of {x} and {y} is {z}");
And what happens is, the variables x, y and z gets replaced with actual values i.e. 5, 8 and 13.