Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - INPUT & OUTPUT


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(...)).


Example :



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.


C# Input


So far, we have used variables to store values. But the variables were loaded with values.


Example :



public class MyApplication
{
    public static void Main(string[] args)
     {
         int x = 9;
         System.Console.WriteLine("The value of x is : "+x);
    }
}


Output :



  The value of x is : 9

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.


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);
    }
}


Output :



  Enter any text : Hello
  The text you have entered is : Hello

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();
C_Sharp


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.

C_Sharp

In the above case, I have typed Hello.


Enter any text : Hello

And Hello goes and sits inside x.

C_Sharp

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.


Get Number as Input


In that case, we can also use System.Console.ReadLine(), but with a minimal change.


Example :



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);
    }
}


Output :



  Enter a number of your choice : 21
  The number you have entered is : 21

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();
C_Sharp


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.

C_Sharp

In the above case, I have typed 21.


Enter any text : 21

And 21 goes and sits inside x.

C_Sharp

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.

C_Sharp

And we got the output,


The text you have entered is : Hello

Next, let us look at C# Output.


C# Output


A simple example of System.Console.WriteLine(...) statement would be,


Example :



public class MyApplication
{
    public static void Main(string[] args)
     {
         int x = 9;
         System.Console.WriteLine("The value of x is : "+x);
    }
}	


Output :



  The value of x is : 9

String Interpolation using '$'


With the System.Console.WriteLine(...) statement, C# supports something called as String Interpolation.


Let us dive into an example to understand String Interpolation.


Example :



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}");
    }
}


Output :



  The added result of 5 and 8 is 13

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.

C_Sharp