Basic .
NET core Application
Unit-3
Console applications: Input/output, command
line arguments
Creating a console application project
Method 1: Using Visual Studio (Easiest for beginners)
Open Visual Studio → “Create a new project”
Choose “Console App” (under C# → Windows → Console)
Select framework:
.NET 8.0 (LTS) or .NET 9.0 (latest in Nov 2025)
Choose type:
Console App → Traditional (uses top-level statements by default)
Console App (.NET Framework) → Only if you need old 4.x framework (rarely used now)
Name your project → e.g., MyFirstConsoleApp
Click “Create”
Method 2: Using Visual Studio Code + Terminal (Lightweight)
Install .NET 9 SDK → [Link]
Install VS Code + “C# Dev Kit” extension
To run code
dotnet new console -n MyFirstConsoleApp // creates the project
dotnet run
Basic console application structure
When you run:
dotnet new console -n MyApp
You get exactly one file with zero boilerplate:
[Link] (Top-Level Statements – This is the entire app)
[Link]("Hello, World!");
You get exactly one file — and that’s perfectly fine!
[Link] (This is the complete application)
// This is a fully working console application
[Link]("Hello, World!");
[Link]("Press any key to exit...");
[Link]();
That’s it! No class, no Main method visible — thanks to Top-Level Statements
(introduced in .NET 6).
We can change to traditional style
using System;
namespace MyBasicApp
{
internal class Program
{
static void Main(string[] args)
{
[Link]("Enter your name: ");
string? name = [Link]();
[Link]($"Hello, {name ?? "User"}!");
}
}
}
Project File (Automatically Created – No Need to Edit)
[Link]
<Project Sdk="[Link]">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Folder Structure (Basic App
MyBasicApp/
├── [Link] ← Your only code file
├── [Link] ← Project file
├── bin/ ← Compiled output (ignore)
└── obj/ ← Build files (ignore)
How to Run
dotnet run # Run directly
dotnet run -- John Alice # Pass arguments
dotnet build # Compile only
dotnet run --no-build # Run without rebuilding
I/O operating in Console
Console input/output is the most basic way for a C# console application to interact
with the use
● [Link](): Writes text without newline [Link]("Hello ");
● [Link](): Writes text with newline [Link]("World!");
● [Link](format, ...): Formatted output (like printf)
[Link]("Age: {0}, Name: {1}", 25, "Ali");
[Link]("=== Basic Output ===");
[Link]("Name: Ahmed");
[Link]($"Year: {[Link]:yyyy}");
[Link]("Progress: {0,0:F1}%", 87.5); // 87.5%
Basic Input (Reading from Console)
● [Link](): string? (whole line) string name = [Link]();
● [Link](): int (ASCII of one char) int key = [Link]();
● [Link](): ConsoleKeyInfo var key = [Link]();
● [Link](true): Reads key without echo For password input
[Link]("Enter your name: ");
string? name = [Link]();
[Link]("Enter your age: ");
int age = Convert.ToInt32([Link]());
[Link]($"Hello {name}, you are {age} years old.");
Using Command-line arguments
Where Do Arguments Come From?
When you run your console app, everything after the executable name is passed
as string arguments.
dotnet run -- John 25 "Software Engineer"
# OR after publishing:
[Link] Alice 30 --verbose
These become the string[] args in your Main method.
Basic Access
[Link]($"You passed {[Link]} argument(s):");
for (int i = 0; i < [Link]; i++)
{
[Link]($" args[{i}] = \"{args[i]}\"");
}
if ([Link] == 0)
{
[Link]("Tip: Try running with your name and age!");
}
Run examples:
dotnet run # Output: You passed 0 argument(s)
dotnet run -- Alice 28 Developer
# Output:
# args[0] = "Alice"
# args[1] = "28"
# args[2] = "Developer"
using System;
class Program
{
static void Main(string[] args)
{
if ([Link] > 0)
{
string name = args[0];
[Link]($"Hello, {name}! Welcome to the console.");
}
else
{
[Link]("Error: Please provide your name as a command-line
argument.");
}
}
}
Error handling and user input validation
Error handling and user input validation are two essential practices that ensure
your console application is robust, reliable, and secure. They work together to
prevent unexpected crashes and maintain data integrity.
Error Handling (Exception Handling)
Error handling is the mechanism used to manage unexpected events (known as
exceptions) that occur while the program is running. It stops the program from
crashing and allows you to implement recovery or logging procedures.
The C# try-catch-finally Structure
User Input Validation
User input validation is the proactive step of checking if the data provided by the
user meets all requirements before the program attempts to use it.
It is the first line of defense against bad data.
Common Types of Validation
● Type/Format Validation: Is the input a number, a date, a currency, etc.?
● Range Validation: Does the value fall within acceptable limits (e.g., an age
between 18 and 65)?
● Presence/Length Validation: Was any data entered, and is it the required
length?
int age = ValidateRange("Age (18-120): ", 18, 120);
public static int ValidateRange(string prompt, int min, int max)
{
while (true)
{
[Link](prompt);
if ([Link]([Link](), out int value) && value >= min &&
value <= max)
return value;
ShowError($"Please enter a number between {min} and {max}.");
}
}
Building and running .NET Core application
Understanding the .NET Core project file (csproj)
<Project Sdk="[Link]">
<PropertyGroup>
<OutputType>Exe</OutputType> <!-- Console app -->
<TargetFramework>net9.0</TargetFramework> <!-- or net8.0, net7.0 -->
<ImplicitUsings>enable</ImplicitUsings> <!-- auto using System, Linq, etc. -->
<Nullable>enable</Nullable> <!-- nullable reference types -->
</PropertyGroup>
</Project>
The .csproj file is the heart of every C# project in modern .NET (from .NET Core to
.NET 9).
It is an XML file that tells the .NET compiler and tools everything they need to
know to build your app.
● Whether it’s a console app or a DLL<OutputType>Exe</OutputType>
● Whether string? null-checking is on<Nullable>enable</Nullable>
● Auto-add common using statements<ImplicitUsings>enable</ImplicitUsings>
● Building the application for different target platforms
● Running the application from command line
● Debugging and Publishing .NET Core application