-
Notifications
You must be signed in to change notification settings - Fork 2
CSharp Basics FirstConsoleApp Input
Joe Care edited this page Dec 22, 2025
·
1 revision
This lesson gives you a first experience with a C# console project and basic user input.
Related wiki pages:
- Overview:
CSharpBible.md - Basics of
Console.WriteLineandDateTime:CSharp-Basics-ConsoleWriteLine-DateTime.md
Official docs:
- Get started with C#: https://learn.microsoft.com/dotnet/csharp/tour-of-csharp/
From a terminal:
dotnet new console -o MyFirstApp
cd MyFirstAppThe template creates a Program.cs with a Main method.
Replace Program.cs with:
using System;
namespace MyFirstApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, C#!");
}
}
}Run it:
dotnet run-
int– whole numbers -
string– text -
bool–true/false
using System;
namespace MyFirstApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Please enter your age: ");
string? input = Console.ReadLine();
if (!int.TryParse(input, out int age))
{
Console.WriteLine("Error: please enter a whole number.");
return;
}
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are not an adult.");
}
}
}
}This combines:
-
Console.ReadLinefor input -
int.TryParsefor safe conversion -
if/elsefor basic branching
For more input/output patterns see Lesson20.12.2025.md (to be renamed to an I/O lesson file).
- Extend this program to be a simple calculator.
- Combine basic types from
Lesson07.12.2025.mdwith input/output from this lesson.