in , ,

C# Basic Input and Output

csharp Basic Input and Output
csharp Basic Input and Output

C# Basic Input and Output: In this tutorial, we will learn how to take input from user and show output in C# using different techniques

C# Output

In order to output something in C#, we can use

System.Console.WriteLine() OR
System.Console.Write()

Here, System is a namespace, Console is a class inside namespace System and WriteLine and Write are strategies for class Console.

Let’s look at a simple example that prints a string to output screen.

Example 1: Printing String using WriteLine()

using System;
 
namespace Sample
{
	class Test
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("C# is cool");
		}
	}
}

At the point when we run the program, the output will be

C# is cool

Difference between WriteLine() and Write() method

The fundamental difference among WriteLine() and Write() is that the Write() strategy just prints the string gave to it, while the WriteLine() technique prints the string and moves to the beginning of next line too.

Let’s take at a look at the example underneath to comprehend the difference between these strategies.

Example 2: How to use WriteLine() and Write() method?

using System;
 
namespace Sample
{
	class Test
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Prints on ");
			Console.WriteLine("New line");

			Console.Write("Prints on ");
			Console.Write("Same line");
		}
	}
}

At the point when we run the program, the output will be

Prints on
New line
Prints on Same line

Printing Variables and Literals using WriteLine() and Write()

The WriteLine() and Write() strategy can be used to print variables and literals. Here’s an example.

Example 3: Printing Variables and Literals

using System;
 
namespace Sample
{
	class Test
	{
		public static void Main(string[] args)
		{
			int value = 10;

			// Variable
			Console.WriteLine(value);
			// Literal
			Console.WriteLine(50.05);
		}
	}
}

At the point when we run the program, the output will be

10
50.05

Combining (Concatenating) two strings using + operator and printing them

Strings can be combined/concatenated using the + operator while printing.

Example 4: Printing Concatenated String using + operator

using System;
 
namespace Sample
{
	class Test
	{
		public static void Main(string[] args)
		{
			int val = 55;
			Console.WriteLine("Hello " + "World");
			Console.WriteLine("Value = " + val);
		}
	}
}

At the point when we run the program, the output will be

Hello World
Value = 55

Printing concatenated string using Formatted String [Better Alternative]

A superior option for printing concatenated string is using formatted string. Formatted string allows programmer to use placeholders for variables. For instance,

The accompanying line,

Console.WriteLine("Value = " + val);

can be replaced by,

Console.WriteLine("Value = {0}", val);

{0} is the placeholder for variable val which will be replaced by worth of val. Since only one variable is used so there is just a single placeholder.

Various variables can be used in the formatted string. We will see that in the example underneath.

Example 5: Printing Concatenated string using String formatting

using System;
 
namespace Sample
{
	class Test
	{
		public static void Main(string[] args)
		{
			int firstNumber = 5, secondNumber = 10, result;
			result = firstNumber + secondNumber;
			Console.WriteLine("{0} + {1} = {2}", firstNumber, secondNumber, result);
		}
	}
}

At the point when we run the program, the output will be

5 + 10 = 15

Here, {0} is replaced by firstNumber, {1} is replaced by secondNumber and {2} is replaced by result. This methodology of printing output is more clear and less error prone than using + operator.

To find out about string formatting, visit C# string formatting.


C# Input

In C#, the least difficult strategy to get input from the user is by using the ReadLine() technique for the Console class. Be that as it may, Read() and ReadKey() are additionally accessible for getting input from the user. They are likewise remembered for Console class.

Example 6: Get String Input From User

using System;
 
namespace Sample
{
	class Test
	{
		public static void Main(string[] args)
		{
			string testString;
			Console.Write("Enter a string - ");
			testString = Console.ReadLine();
			Console.WriteLine("You entered '{0}'", testString);
		}
	}
}

At the point when we run the program, the output will be:

Enter a string - Hello World
You entered 'Hello World'

Difference between ReadLine(), Read() and ReadKey() method:

The difference between ReadLine(), Read() and ReadKey() strategy is:

• ReadLine(): The ReadLine() strategy reads the next line of input from the standard input stream. It returns a similar string.

• Read(): The Read() strategy reads the next character from the standard input stream. It returns the ascii value of the character.

• ReadKey(): The ReadKey() technique obtains the next key pressed by user. This strategy is generally used to hold the screen until user press a key.

In the event that you need to find out about these strategies, here is a fascinating discussion on StackOverflow on: Difference between Console.Read() and Console.ReadLine()?.


Example 7: Difference between Read() and ReadKey() method

using System;
 
namespace Sample
{
	class Test
	{
		public static void Main(string[] args)
		{
			int userInput;

			Console.WriteLine("Press any key to continue...");
			Console.ReadKey();
			Console.WriteLine();

			Console.Write("Input using Read() - ");
			userInput = Console.Read();
			Console.WriteLine("Ascii Value = {0}",userInput);
		}
	}
}

At the point when we run the program, the output will be

Press any key to continue...
x
Input using Read() - Learning C#
Ascii Value = 76

From this example, it should be clear how ReadKey() and Read() technique works. While using ReadKey(), when the key is pressed, it is shown on the screen.

At the point when Read() is used, it takes an entire line but just returns the ASCII value of first character. Henceforth, 76 (ASCII value of L) is printed.


Reading numeric values (integer and floating point types)

Reading a character or string is very straightforward in C#. You should simply call the corresponding strategies as required.

In any case, reading numeric values can be somewhat interesting in C#. We’ll in any case use a similar ReadLine() technique we used for getting string values. In any case, since the ReadLine() technique gets the input as string, it should be changed over into integer or floating point type.

One basic methodology for changing over our input is using the strategies for Convert class.

Example 8: Reading Numeric Values from User using Convert class

using System;
 
namespace UserInput
{
	class MyClass
	{
		public static void Main(string[] args)
		{
			string userInput;
			int intVal;
			double doubleVal;

			Console.Write("Enter integer value: ");
			userInput = Console.ReadLine();
			/* Converts to integer type */
			intVal = Convert.ToInt32(userInput);
			Console.WriteLine("You entered {0}",intVal);

			Console.Write("Enter double value: ");
			userInput = Console.ReadLine();
			/* Converts to double type */
			doubleVal = Convert.ToDouble(userInput);
			Console.WriteLine("You entered {0}",doubleVal);
		}
	}
}

At the point when we run the program, the output will be

Enter integer value: 101
You entered 101
Enter double value: 59.412
You entered 59.412

The ToInt32() and ToDouble() technique for Convert class changes over the string input to integer and double sort individually. Likewise we can change the input over to different types. Here is a complete list of available methods for Convert class.

There are other ways to get numeric inputs from user. To learn more, visit Reading an integer from user input.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

csharp Operators

C# Operators

csharp Expressions, Statements and Blocks_

C# Expressions, Statements and Blocks (With Examples)