Console App

A console application is a program that can only display text output.


The main method can be defined in any valid class


The main() method is the entry point for every C# console application.
The main method can be declared with or without a string[] parameter.

namespace ConsoleApp 
   internal class Program
   {
     static void Main(string[] args)
     {
        System.Console.WriteLine("Better Solutions")
     }
   }
}

Starting in .NET 5.0 you can omit the Main method


The last line should be Console.ReadLine() - this causes the program to pause and display any results



WriteLine - displays a line of text followed by a carriage return
Write - displays a line of text
ReadLine - pauses the program to wait for input and the Enter key to be pressed


use {0} to indicate a placeholder for the first variable's value


Select (Debug > Start) then the command window is automatically closed after the program finishes
Select (Debug > Start with Debugging) will leave the command window open to allow you to view the programs output.


You can display multiple lines by using the newline character ("\n")


If System.Console.WriteLine does not receive a text string then it will output a newline character.


Escape Characters

\n - newline, positions the cursor at the beginning of the next line
\t - horizontal tab, moves he cursor to the next tab stop
\r - carriage return, positions the cursor at the start of the current line ?
\\ backslash, prints a backslash character
\* double quote, prints a double quote character



Console Application

Create a New Project
Find "Console App (with C#) and press Next.
Change the Project Name to "ConsoleApp1"
Change the Location by clicking on the Browse button.
Tick the "Place Solution and project in the same directory" checkbox.
Press Next
The Console App Additional Information will appear
Select .NET 6.0 as your target framework
Press Create


This will create a solution that contains one project
ConsoleApp1
SS
The project contains one file called "Program.cs"

Console.WriteLine("Hello World!"); 

Press F5 to run the program.
The Microsoft Visual Studio Debug Console will appear with the text "Hello World".
Press any key to close the console.




© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext