Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C# Fundamentals: Input and Output Essentials

C# Fundamentals: Input and Output Essentials

This guide walks you through C#’s core input and output techniques, showing how to read user data and display results reliably.

C# Output

To write data to the console, C# offers two primary methods:

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

Here, System is the namespace, Console is a class within that namespace, and WriteLine and Write are its methods.

Example 1: Printing a String with WriteLine()

using System;

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

Running the program produces:

C# is cool

Difference Between WriteLine() and Write()

WriteLine() outputs the supplied text and advances to a new line, while Write() prints text without adding a line break.

Example 2: Using WriteLine() and Write()

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

Output:

Prints on
New line
Prints on Same line

Printing Variables and Literals

Both WriteLine() and Write() can display variables and literals.

Example 3: 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);
        }
    }
}

Result:

10
50.05

Concatenating Strings with the + Operator

Strings can be combined using the + operator during output.

Example 4: Concatenated Output

using System;

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

Result:

Hello World
Value = 55

Formatted Strings – A Cleaner Approach

Formatted strings let you embed placeholders that are replaced at runtime, improving readability and reducing errors.

For example, instead of:

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

use:

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

Here, {0} is a placeholder for val. Multiple variables are handled with additional placeholders.

Example 5: 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);
        }
    }
}

Output:

5 + 10 = 15

This method is more readable and less error‑prone than manual concatenation.

Learn more about string formatting in C# string formatting.


C# Input

The most straightforward way to capture user input is Console.ReadLine(). For more granular input, Read() and ReadKey() are also available.

Example 6: Reading a String from the 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);
        }
    }
}

Output:

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

Understanding ReadLine(), Read(), and ReadKey()

For deeper insight, see the discussion on StackOverflow titled “Difference between Console.Read() and Console.ReadLine?”.


Example 7: Demonstrating Read() and ReadKey()

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

Output:

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

Notice that ReadKey() displays the key immediately, while Read() captures a line but only returns the first character’s ASCII code (here, 76 for ‘L’).


Reading Numeric Values

While strings are read directly, numeric types require conversion. The Convert class provides straightforward methods.

Example 8: Converting User Input to Numbers

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();
            intVal = Convert.ToInt32(userInput);
            Console.WriteLine("You entered {0}", intVal);

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

Result:

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

Convert’s ToInt32() and ToDouble() methods transform the input string into the desired numeric type. A full list of conversion methods is available in the .NET documentation.

Explore alternative numeric input strategies in Reading an integer from user input.

C Language

  1. Expanding Cloud Horizons: New Zones, Containers, and Multicloud Strategies
  2. Input & Output Coupling Techniques for Amplifiers: Capacitive, Direct, and Transformer Methods
  3. Mastering C++ Input and Output: A Practical Guide
  4. Mastering C Input and Output (I/O): scanf() and printf() Explained
  5. Mastering Python I/O and Module Imports: A Practical Guide
  6. Master Java Input & Output: Print, Read, and Format Your Data
  7. Mastering Xilinx Vivado: Integrated Logic Analyzer (ILA) & Virtual Input/Output (VIO) Debugging
  8. Mastering C++ Streams: cout, cin, cerr, clog – Practical Guide
  9. Mastering D Latches: Design, Operation, and Key Differences
  10. Mastering Input and Output in C Programming