Algorithm


Below is a list of basic input and output operations in C#:

Input:

  1. Console.ReadLine():

    • Reads a line of text from the console as a string.
    csharp
    string input = Console.ReadLine();
  • Console.Read():

    • Reads the next character from the standard input.
    csharp
    int input = Console.Read();
  • Console.ReadKey():

    • Obtains the next key pressed by the user, including special keys.
    csharp
    ConsoleKeyInfo keyInfo = Console.ReadKey();
    char input = keyInfo.KeyChar;

Output:

  1. Console.Write():

    • Writes the specified string to the console without a newline character.
    csharp
    Console.Write("Hello, ");
    Console.Write("World!");
  • Console.WriteLine():

    • Writes the specified string to the console with a newline character.
    csharp
    Console.WriteLine("Hello, World!");
  • String Interpolation:

    • Embeds expressions into string literals.
    csharp
    int age = 25;
    Console.WriteLine($"My age is {age}.");
  • Formatting Output:

    • Using String.Format for formatted output.
    csharp
    int x = 5, y = 10;
    Console.WriteLine("The sum of {0} and {1} is {2}", x, y, x + y);
  • Escape Sequences:

    • Using escape sequences for special characters.
    csharp
    Console.WriteLine("This is a new line.\n\tThis is a tab.");
  • Console.Clear():

    • Clears the console screen.
    csharp
    Console.Clear();

These are fundamental input and output operations that you can use to create basic console-based programs in C#.

Code Examples

#1 Printing String using WriteLine() in C# Programming

Code - C# Programming

using System;
 
namespace Sample
{
	class Test
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("C# is cool");
		}
	}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
C# is cool

#2 Difference between WriteLine() and Write() method in C# Programming

Code - C# Programming

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");
		}
	}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Prints on
New line
Prints on Same line

#3 Printing Variables and Literals in C# Programming

Code - C# Programming

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

			// Variable
			Console.WriteLine(value);
			// Literal
			Console.WriteLine(50.05);
		}
	}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
10
50.05

#4 Printing Concatenated String using + operator in C# Programming

Code - C# Programming

using System;
 
namespace Sample
{
	class Test
	{
		public static void Main(string[] args)
		{
			int val = 55;
			Console.WriteLine("Hello " + "World");
			Console.WriteLine("Value = " + val);
		}
	}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Hello World
Value = 55

#5 Printing Concatenated string using String formatting in C# programming

Code - C# Programming

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);
		}
	}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
5 + 10 = 15

#6 Get String Input From User in C# Programming

Code - C# Programming

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);
		}
	}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter a string - Hello World
You entered 'Hello World'

#7 Difference between Read() and ReadKey() method in C# Programming

Code - C# Programming

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);
		}
	}
}
Copy The Code & Try With Live Editor

Output

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

#8 Reading Numeric Values from User using Convert class in C# Programming

Code - C# Programming

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);
		}
	}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter integer value: 101
You entered 101
Enter double value: 59.412
You entered 59.412
Advertisements

Demonstration


C# Programming Basic Input and Output