C# Basics for Beginners
C# (pronounced "C-sharp") is the most popular language on the .NET platform. It is a statically typed, object-oriented language that reads cleanly and gives helpful compiler errors, which makes it a great first language. This guide covers the absolute basics: how a program is structured, how to declare variables, how to read input and print output, and how to make decisions with if and loops. All examples target modern .NET (C# 14).
A minimal C# program
Modern C# supports top-level statements, so a tiny program needs no class or Main method:
// Program.cs
// Console.WriteLine prints a line of text to the terminal.
Console.WriteLine("Welcome to C#!");
Run it with dotnet run. Every C# statement ends with a semicolon ;.
Variables and types
A variable is a named box that holds a value. C# is statically typed, meaning each variable has a type fixed at compile time:
int age = 19; // whole number
double marks = 84.5; // number with a decimal point
string city = "Jalgaon"; // text
bool isEnrolled = true; // true or false
// 'var' lets the compiler infer the type from the value on the right.
var greeting = "Hi"; // compiler knows this is a string
Console.WriteLine(age); // 19
Console.WriteLine(greeting); // Hi
var is just a shortcut — the type is still fixed once assigned. You cannot later put a number into a string variable.
Output and input
Use Console.WriteLine to print and Console.ReadLine to read a line the user types:
Console.Write("Enter your name: "); // Write (no new line) keeps the cursor on the same line
string? name = Console.ReadLine(); // ReadLine returns the text the user typed (may be null)
// String interpolation: the value of 'name' is placed inside the text.
Console.WriteLine($"Hello, {name}!");
Console.ReadLine always returns text. To get a number you must convert it:
Console.Write("Enter your age: ");
string? input = Console.ReadLine();
int age = int.Parse(input!); // convert text "20" into the number 20
Console.WriteLine($"Next year you'll be {age + 1}.");
For safer conversion that won't crash on bad input, use int.TryParse (covered in the data-types article).
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesOperators
int a = 10, b = 3;
Console.WriteLine(a + b); // 13 addition
Console.WriteLine(a - b); // 7 subtraction
Console.WriteLine(a * b); // 30 multiplication
Console.WriteLine(a / b); // 3 integer division (drops the remainder)
Console.WriteLine(a % b); // 1 modulus (the remainder)
Note that 10 / 3 gives 3, not 3.33, because both numbers are integers. Use double for decimal division.
Making decisions and looping
int score = 72;
// if / else if / else chooses one branch to run.
if (score >= 75)
Console.WriteLine("Distinction");
else if (score >= 40)
Console.WriteLine("Pass");
else
Console.WriteLine("Try again");
// A for loop repeats a block a known number of times.
for (int i = 1; i <= 3; i++)
{
Console.WriteLine($"Attempt {i}");
}
// Output: Attempt 1 / Attempt 2 / Attempt 3
The == operator checks equality, while = assigns a value — a classic beginner trap.
Common mistakes
- Using
=instead of==in conditions.if (x = 5)is wrong; useif (x == 5). C# will usually catch this forbool, but the habit matters. - Forgetting the semicolon. Every statement ends with
;. Missing one is the most common compile error. - Treating
Console.ReadLineoutput as a number. It is always text; convert it withint.Parseorint.TryParse. - Integer division surprise.
5 / 2is2, not2.5. Make at least one operand adouble. - C# is case-sensitive.
Nameandnameare different variables.
FAQ
Is C# the same as C or C++? No. They share some syntax, but C# is a higher-level, memory-managed language that runs on .NET.
Do I need to memorize all the types? No — start with int, double, string, and bool. The rest come naturally.
Keep learning
- Hub: Learn .NET
- Next: C# Data Types & Variables
- Related: What is .NET?
Ready to practise with a mentor in Jalgaon? Join the waitlist for the Infoplanet .NET course at /courses/dotnet.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Data Access with ADO.NET
Connect to and query a database from C# with ADO.NET — connections, commands, parameterized queries, and data readers — written safely with using blocks.
Introduction to ASP.NET Core
Understand ASP.NET Core — the modern, cross-platform web framework for .NET — and build your first minimal web API with routing and JSON responses.
ASP.NET: Web Forms vs MVC vs Razor Pages
Understand the difference between ASP.NET Web Forms, MVC, and Razor Pages — including why Web Forms is legacy — to pick the right model on modern .NET.
