.NET HELP

C# Writeline (How It Works For Developers)

Published January 14, 2024
Share:

What is a Console Window?

The console is a window in the operating system where users may enter text such as hello world string using the computer keyboard in the new or same line and view text output from the computer terminal to interact with the system or a text-based console application. For instance, under the Windows operating system, MS-DOS instructions may be entered into a console known as the Command Prompt window. Applications that read and write characters to the console are supported fundamentally by the Console class. In this article, we are going to use the WriteLine method within static void main in C#.

How to use C# WriteLine

  1. Create a new C# project.
  2. Make sure the current .NET version has been installed.
  3. Use any one of the write methods.
  4. Display the output based on the requirement.
  5. Run the code.

What is WriteLine?

The console window may be made to show a line of text followed by a newline by using the WriteLine() function. This function is a part of the Console output class, which is a component of the System namespace and offers functions for working with the standard error, input value, and output streams.

  • Console: The standard input, output, and error streams of an application are represented by this C# class, which is found in the System namespace.
  • WriteLine: This function writes a newline character and the provided text or data to the console. It shows the content and then advances the pointer to the start of the following line. The only difference between the WriteLine and the Write method is the new line.

Syntax

Console.WriteLine(); // console line output
Console.WriteLine(string value); // write value
Console.WriteLine(string format, params object [] args);
Console.WriteLine(); // console line output
Console.WriteLine(string value); // write value
Console.WriteLine(string format, params object [] args);
Console.WriteLine() ' console line output
Console.WriteLine(String value) ' write value
Console.WriteLine(String format, params Object () args)
VB   C#

Parameters

  • value (optional): This is a representation of the data or text that you wish to see on the console. A string, a variable, or a mix of strings and variables may be used.
  • format: A string with format requirements (optional). Placeholders like {0}, {1}, and so forth can be included; they are substituted with the appropriate parameters listed in the args parameter.
  • args (optional): The composite format string arguments in the format parameter that match the placeholders. The placeholders will determine how these parameters are represented within the string.

Functionality

  • The Console is the text output. The console window may be used to show text or other data when using the WriteLine() function.
  • Newline: After displaying the material, it automatically appends a newline character (\n). This guarantees that each output after that will begin on a new line in the console.
  • Format string: String interpolation ($"), formatting placeholders ({0}, {1}, etc.), and formatting options ({1:C} for currency, {0:D} for the date, etc.) may all be used to create formatted output.
  • Variable show: By converting variables to their string representations, it may show variables of different data kinds, including string, integer, double, and so on.
  • Overloads and Different Data Kinds: The function can accept integers, doubles, booleans, characters, objects, etc. since it has several overloads available to handle different data kinds.
  • Special Characters and Escape Sequences: you may use escape sequences for tabs \t, newlines \n, and other special characters.

Concat using Console.WriteLine()

In C#, concatenation is the process of joining variables or strings into a single string. Concatenation may be utilized with Console. To see concatenated texts or a combination of strings and variables in the console, use WriteLine().

Here's an example using Console to show concatenation.

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string [] args)
        {
            String Name = "Jack";
            // Example for concatenating strings and variables using the + operator
            Console.WriteLine("Hello "+Name);
            // Using string interpolation to concatenate strings and variables
            Console.WriteLine($"Hello {Name}");
            // Using placeholders and formatting to concatenate strings and variables on same line
            Console.Write("Hello {0}",Name);
        }
    }
}
namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string [] args)
        {
            String Name = "Jack";
            // Example for concatenating strings and variables using the + operator
            Console.WriteLine("Hello "+Name);
            // Using string interpolation to concatenate strings and variables
            Console.WriteLine($"Hello {Name}");
            // Using placeholders and formatting to concatenate strings and variables on same line
            Console.Write("Hello {0}",Name);
        }
    }
}
Namespace ConsoleApp1
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			Dim Name As String = "Jack"
			' Example for concatenating strings and variables using the + operator
			Console.WriteLine("Hello " & Name)
			' Using string interpolation to concatenate strings and variables
			Console.WriteLine($"Hello {Name}")
			' Using placeholders and formatting to concatenate strings and variables on same line
			Console.Write("Hello {0}",Name)
		End Sub
	End Class
End Namespace
VB   C#

In the above example:

  • The + operator, string interpolation ($"), and formatting placeholders like {0}, {1}, etc. are used to concatenate strings and variables.
  • Concatenated strings, variables, and even newlines (\n) for line breaks can all be shown using the system WriteLine() function.
  • Within the Console, there are many methods for concatenating text and variables. In C#, use WriteLine() to send formatted messages or data to the console in the code.

A crucial C# function for console-based input/output tasks is WriteLine(). It is a flexible tool for interaction and communication within console programs because of its capacity to handle several data kinds, apply formatting, and output text or values to the console window.

IronPDF with WriteLine

Install IronPDF

Obtain the IronPDF library; it is necessary for the next patch. Enter the subsequent code into the Package Manager to perform this:

Install-Package IronPdf

C# Writeline (How It Works For Developer): Figure 1 - Install IronPDF

As an alternative, you may look for the package "IronPDF" using the NuGet Package Manager. This list of all the NuGet packages related to IronPDF allows us to select and download the required package.

C# Writeline (How It Works For Developer): Figure 2 - IronPDF Package

WriteLine in IronPDF

The sample code demonstrates how to use the string interpolation function to produce a PDF and display the process status with the WriteLine method. Format strings and alignment specifiers can be concatenated for a single interpolation statement.

using IronPdf;
namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string [] args)
        {
            int x = 25;
            var outputstr = $@"square of <b>{x}</b> is <b>{Math.Sqrt(x)}</b>";
            Console.WriteLine($"Ironpdf Process started at {DateTime.Now.ToString("hh:mm:ss:ffff")}");
            var pdfcreate = ChromePdfRenderer.StaticRenderHtmlAsPdf(outputstr);
            pdfcreate.SaveAs("demo.pdf");
            Console.WriteLine($"Ironpdf Process End at {DateTime.Now.ToString("hh:mm:ss:ffff")}");
        }
    }
}
using IronPdf;
namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string [] args)
        {
            int x = 25;
            var outputstr = $@"square of <b>{x}</b> is <b>{Math.Sqrt(x)}</b>";
            Console.WriteLine($"Ironpdf Process started at {DateTime.Now.ToString("hh:mm:ss:ffff")}");
            var pdfcreate = ChromePdfRenderer.StaticRenderHtmlAsPdf(outputstr);
            pdfcreate.SaveAs("demo.pdf");
            Console.WriteLine($"Ironpdf Process End at {DateTime.Now.ToString("hh:mm:ss:ffff")}");
        }
    }
}
Imports IronPdf
Namespace ConsoleApp1
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			Dim x As Integer = 25
			Dim outputstr = $"square of <b>{x}</b> is <b>{Math.Sqrt(x)}</b>"
			Console.WriteLine($"Ironpdf Process started at {DateTime.Now.ToString("hh:mm:ss:ffff")}")
			Dim pdfcreate = ChromePdfRenderer.StaticRenderHtmlAsPdf(outputstr)
			pdfcreate.SaveAs("demo.pdf")
			Console.WriteLine($"Ironpdf Process End at {DateTime.Now.ToString("hh:mm:ss:ffff")}")
		End Sub
	End Class
End Namespace
VB   C#

In the above example, we are creating the PDF file. We are monitoring the process status with the help of the write method print values of the process started time by converting with the help of the ToString method.

Console Output:

C# Writeline (How It Works For Developer): Figure 3 - Console Output

PDF Result:

C# Writeline (How It Works For Developer): Figure 4 - PDF Output

To read more about the IronPDF refer here.

Conclusion

In conclusion, the WriteLine function in C# is a vital tool for developers as it is key to the process of writing data object to the console. Complex output patterns, formatted texts, and a variety of data kinds may all be shown because of their flexibility and simplicity. WriteLine offers a simple way to communicate in the terminal environment, which makes debugging, testing, and user interaction easier.

The IronPDF price starts at a $749 Lite package that includes a permanent license, upgrade options, one year of software maintenance, and a thirty-day money-back guarantee. During the watermarked trial period, users can assess the product in real-world application scenarios for thirty days. To find out more about IronPDF's price, licensing, and trial version, click the provided link. To learn more about Iron Software products, check here.

< PREVIOUS
Test Console Application C# (How It Works For Developers)
NEXT >
C# String Interpolation (How It Works For Developers)

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 11,308,499 View Licenses >