Learn C# ProgrammingNew
• Updated 3/12/2026 • C#programmingbeginner programmingsoftware developmentcoding basics
Learn C# Programming
Home
Welcome to Learn C# Programming. This tutorial is made for complete beginners. It uses simple words, short examples, and step-by-step explanations.
C# is pronounced C-Sharp. It is a programming language made by Microsoft. People use it to build games, websites, mobile apps, desktop tools, and business software.
Programming means writing instructions for a computer. The computer follows those instructions exactly. If the instructions are clear, the program works. If the instructions are wrong, the program gives an error or does something unexpected.
Think of programming like writing a recipe. A recipe tells a person what to do step by step. A program tells a computer what to do step by step.
This tutorial will help you learn the basic building blocks of C# so you can begin writing your own small programs with confidence.
Overview
A computer program is a set of instructions. These instructions tell the computer how to solve a problem or complete a task.
Most programs follow a simple pattern:
- Get input
- Process data
- Show output
For example, a simple calculator program might:
- Ask the user for two numbers
- Add the numbers together
- Display the answer
Here is a very small C# program:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Welcome to C#");
}
}
This program prints text on the screen. Even though it is small, it already shows some important parts of C#.
- using System; gives access to useful built-in tools
- class Program creates a class named Program
- static void Main() is the starting point of the program
- Console.WriteLine() prints text to the console
When a C# program runs, it starts in the Main() method. That is the first place the computer looks for instructions.
Basics
C# programs are made from several pieces that work together.
Some important pieces are:
- Namespaces
- Classes
- Methods
- Statements
- Variables
- Expressions
A statement is a single instruction. A method is a block of instructions grouped together. A class is a structure that can hold data and methods.
Look at this example:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Program started");
Console.WriteLine("Learning C# step by step");
}
}
Each Console.WriteLine() line is a statement. The computer runs them from top to bottom.
Programs usually become easier to understand when they are broken into small parts. That is why methods and classes are important. They help organize the code.
Environment
Before you can write and run C# code, you need a working setup. This setup is called the development environment.
To start learning C#, you usually need:
- The .NET SDK
- A code editor, such as Visual Studio or Visual Studio Code
- A terminal or command prompt
A common beginner workflow looks like this:
- Open your editor
- Create a new C# file or project
- Write your code
- Save the file
- Build or compile the program
- Run the program
Compile means changing your C# code into a form the computer can execute.
Run means starting the program so the computer follows your instructions.
Many beginners like using the console first because it is simple. A console program shows text and can also read text input from the user.
For example:
using System;
class Program
{
static void Main()
{
Console.WriteLine("My first console app");
}
}
Basic Syntax
Syntax means the rules for writing code correctly. Every programming language has its own syntax rules.
Important syntax rules in C#:
- Statements usually end with a semicolon ;
- Curly braces { } mark blocks of code
- C# is case-sensitive
- Words like main and Main are not the same
- Indentation is not required for the computer, but it makes code much easier to read
Example:
Console.WriteLine("Hello");
Console.WriteLine("C# is fun");
This works because each statement ends with a semicolon.
Now look at a block of code:
if (true)
{
Console.WriteLine("This block runs");
}
The braces show which statements belong to the if block.
Good formatting helps you find mistakes faster and understand your own code more easily.
Data Types
A computer stores data in memory. A data type tells the computer what kind of data a variable holds.
Common C# data types:
- int for whole numbers
- double for decimal numbers
- float for decimal numbers with less precision
- char for one character
- string for text
- bool for true or false values
- long for very large whole numbers
- decimal for precise decimal values, often used for money
Example:
int age = 13;
double height = 1.55;
char grade = 'A';
string name = "Mia";
bool isStudent = true;
Using the correct data type matters. If you want to store a name, use string. If you want to store a number like 25, use int. If you want to store a number like 3.14, use double.
Choosing the correct type helps your program work correctly and clearly.
Variables
A variable is a named storage place in memory. It lets your program remember values.
When creating a variable, you usually write:
- The data type
- The variable name
- The value
Example:
int score = 100;
string playerName = "Alex";
bool gameOver = false;
You can use variables in output:
Console.WriteLine(playerName);
Console.WriteLine(score);
You can also change a variable later:
int lives = 3;
lives = 2;
Console.WriteLine(lives);
This is called assignment. The new value replaces the old one.
Good variable names make code easier to understand. Compare these:
int x = 10;
int studentAge = 10;
studentAge is much clearer than x.
Keywords
Keywords are special words that already have a meaning in C#. Because they are reserved by the language, you cannot use them as normal variable names.
Examples of C# keywords:
- class
- int
- string
- void
- public
- private
- static
- if
- else
- for
- while
- return
- new
- true
- false
Example:
using System;
class Game
{
public string Title = "Space Adventure";
public int Score = 0;
public void StartGame()
{
Console.WriteLine("Game Started");
}
public void AddScore(int points)
{
Score = Score + points;
}
public void ShowScore()
{
Console.WriteLine("Current Score: " + Score);
}
}
class Program
{
static void Main()
{
Game myGame = new Game();
Console.WriteLine("Game Title: " + myGame.Title);
myGame.StartGame();
myGame.AddScore(10);
myGame.AddScore(5);
myGame.ShowScore();
}
}
Here, class is a keyword. It tells C# that you are creating a class.
You should learn to recognize common keywords because they appear often in programs.
Operators
Operators are symbols that perform actions on values.
Arithmetic operators:
- + add
- - subtract
- * multiply
- / divide
- % remainder
int a = 10;
int b = 3;
Console.WriteLine(a + b);
Console.WriteLine(a - b);
Console.WriteLine(a * b);
Console.WriteLine(a / b);
Console.WriteLine(a % b);
Comparison operators compare values:
- == equal to
- != not equal to
- > greater than
- < less than
- >= greater than or equal to
- <= less than or equal to
Logical operators are used with true and false values:
- && and
- || or
- ! not
int age = 15;
bool hasTicket = true;
Console.WriteLine(age >= 13 && hasTicket);
Assignment operators set values:
- = assign
- += add and assign
- -= subtract and assign
int score = 10;
score += 5;
Console.WriteLine(score);
Decisions
Programs often need to choose between different actions. This is called decision making.
The most common decision tool is the if statement.
int age = 15;
if (age >= 13)
{
Console.WriteLine("Teenager");
}
If the condition is true, the code inside the block runs.
You can also use else:
int score = 80;
if (score >= 50)
{
Console.WriteLine("Passed");
}
else
{
Console.WriteLine("Failed");
}
You can check more than one condition using else if:
int grade = 85;
if (grade >= 90)
{
Console.WriteLine("Excellent");
}
else if (grade >= 75)
{
Console.WriteLine("Good");
}
else
{
Console.WriteLine("Keep practicing");
}
C# also has a switch statement, which is useful when one value can match several choices.
int day = 2;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
default:
Console.WriteLine("Other day");
break;
}
Loops
Loops repeat code. They are very useful when you want to do something many times.
The for loop is used when you know how many times to repeat.
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
The while loop repeats while a condition stays true.
int count = 1;
while (count <= 3)
{
Console.WriteLine(count);
count++;
}
The do-while loop is similar, but it runs the block at least one time.
int number = 1;
do
{
Console.WriteLine(number);
number++;
}
while (number <= 3);
The foreach loop is used for collections like arrays.
int[] values = { 4, 7, 9 };
foreach (int value in values)
{
Console.WriteLine(value);
}
Loops are powerful, but be careful. If the condition never becomes false, the loop may run forever. That is called an infinite loop.
Numbers
Numbers are very important in programming. C# has several number types for different needs.
- int for whole numbers
- long for larger whole numbers
- double for decimal numbers
- float for smaller decimal values
- decimal for very accurate decimal values
Example:
int apples = 5;
double weight = 2.5;
decimal price = 19.99m;
Console.WriteLine(apples);
Console.WriteLine(weight);
Console.WriteLine(price);
You can do calculations with numbers:
int result = 10 + 5;
int total = 20 - 4;
int product = 3 * 6;
int quotient = 12 / 3;
Console.WriteLine(result);
Console.WriteLine(total);
Console.WriteLine(product);
Console.WriteLine(quotient);
Division with integers can surprise beginners:
Console.WriteLine(5 / 2);
This gives 2, not 2.5, because both numbers are integers. To get a decimal answer, use decimal numbers:
Console.WriteLine(5.0 / 2);
Characters
A char stores one single character. This can be a letter, number, or symbol.
char letter = 'A';
char digit = '7';
char symbol = '#';
Console.WriteLine(letter);
Console.WriteLine(digit);
Console.WriteLine(symbol);
A character uses single quotes.
This is a character:
char grade = 'B';
This is a string:
string gradeText = "B";
They may look similar, but they are different data types.
Arrays
An array stores multiple values of the same type in one variable.
Arrays are useful when you want to keep a list of related data.
int[] numbers = { 1, 2, 3, 4 };
You access items using indexes. The first item is at index 0.
Console.WriteLine(numbers[0]);
Console.WriteLine(numbers[1]);
You can change an item:
numbers[2] = 99;
Console.WriteLine(numbers[2]);
You can check the size of an array using Length:
Console.WriteLine(numbers.Length);
You can loop through an array:
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
Or with foreach:
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Strings
A string stores text. Strings are used for names, messages, labels, and sentences.
string message = "Hello World";
Console.WriteLine(message);
You can find the length of a string:
Console.WriteLine(message.Length);
You can join strings together. This is called concatenation.
string firstName = "Lia";
string lastName = "Santos";
string fullName = firstName + " " + lastName;
Console.WriteLine(fullName);
You can also use string interpolation, which many beginners find easier to read:
string name = "Noah";
int age = 13;
Console.WriteLine($"My name is {name} and I am {age} years old.");
Strings have useful built-in methods:
- ToUpper() makes text uppercase
- ToLower() makes text lowercase
- Contains() checks if text exists inside a string
- Replace() changes part of the text
string text = "C# Programming";
Console.WriteLine(text.ToUpper());
Console.WriteLine(text.Contains("C#"));
Console.WriteLine(text.Replace("Programming", "Basics"));
Functions
A function, also called a method in C#, is a reusable block of code.
Functions help you avoid repeating the same instructions again and again.
Here is a simple function:
static void SayHello()
{
Console.WriteLine("Hello");
}
To run it, call it:
SayHello();
A function can also receive information. This information is called a parameter.
static void Greet(string name)
{
Console.WriteLine("Hello " + name);
}
Calling it:
Greet("Mia");
Greet("Ken");
A function can also return a value using return.
static int Add(int a, int b)
{
return a + b;
}
Using the returned value:
int answer = Add(4, 6);
Console.WriteLine(answer);
Functions make programs cleaner, easier to test, and easier to understand.
File I/O
File I/O means reading from files and writing to files. This topic is useful when a program needs to save information for later.
For example, a program could save a username, a score, or a note into a file.
To work with files, C# often uses tools from System.IO.
using System;
using System.IO;
class Program
{
static void Main()
{
File.WriteAllText("data.txt", "Hello File");
string text = File.ReadAllText("data.txt");
Console.WriteLine(text);
}
}
In this example:
- File.WriteAllText() creates a file and writes text into it
- File.ReadAllText() reads all the text from the file
You can also add text to an existing file:
File.AppendAllText("data.txt", "\nMore text");
When learning file handling, it is important to remember that files may not always exist, so real programs often check carefully before reading.
OOP Concepts
OOP stands for Object-Oriented Programming. It is a way of organizing code using objects.
An object can represent a real thing, like a dog, a car, or a student. It can also represent something from a game or app, like a player or an item.
Main OOP ideas:
- Class - a blueprint
- Object - an instance created from a class
- Encapsulation - keeping data and behavior together
- Inheritance - creating a new class from an existing class
- Polymorphism - letting different objects behave in their own way
OOP helps keep big programs organized. It also makes code easier to reuse and improve.
For beginners, the most important first step is understanding classes and objects.
Class and Object
A class is a blueprint. It describes what data something has and what actions it can do.
An object is a real item created from that blueprint.
Example class:
class Dog
{
public string Name;
public int Age;
public void Bark()
{
Console.WriteLine("Woof");
}
}
Creating an object:
Dog myDog = new Dog();
myDog.Name = "Buddy";
myDog.Age = 3;
Console.WriteLine(myDog.Name);
Console.WriteLine(myDog.Age);
myDog.Bark();
In this example:
- Dog is the class
- myDog is the object
- Name and Age are data stored in the object
- Bark() is an action the object can perform
Classes help us model real things in code.
Dynamic Memory Management
When a program runs, it uses memory to store data. In some languages, programmers must manage memory very carefully by hand. In C#, much of this work is done automatically.
C# uses something called the garbage collector. The garbage collector looks for objects that are no longer being used and removes them from memory.
This helps prevent many common memory mistakes.
Example:
class Player
{
public string Name;
}
Player p = new Player();
p.Name = "John";
Console.WriteLine(p.Name);
When the object is no longer needed and nothing is using it anymore, C# can clean it up later.
As a beginner, the key idea is simple:
- Creating objects uses memory
- Unused objects can be cleaned automatically
- C# helps manage this for you
This does not mean memory never matters, but it makes C# easier to learn than some lower-level languages.
Algorithms and Their Complexity
An algorithm is a step-by-step method for solving a problem.
Every time you sort numbers, search a list, or calculate a result, you are using an algorithm.
Here is an example algorithm that finds the largest number in an array:
int[] numbers = { 4, 9, 2, 7 };
int max = numbers[0];
for (int i = 1; i < numbers.Length; i++)
{
if (numbers[i] > max)
{
max = numbers[i];
}
}
Console.WriteLine(max);
This algorithm starts by assuming the first number is the largest. Then it checks the rest one by one.
Complexity means how the amount of work grows as the amount of data grows.
For beginners, think of it like this:
- If a list gets bigger, does the program take a little more time or much more time?
- Some algorithms stay efficient as data grows
- Some algorithms become slow when data gets large
The example above checks each number once, so it is a simple and efficient approach for finding the maximum value.
You do not need advanced math yet. Just remember that good algorithms save time and work.
Summary
You have now learned the core beginner ideas of C# programming.
You learned that a program is a set of instructions. You learned how C# uses classes, methods, variables, data types, decisions, loops, arrays, strings, and functions.
You also saw how C# can work with files, how object-oriented programming organizes code, how classes create objects, how memory is handled automatically, and why algorithms matter.
The best way to improve is to practice. Write small programs often. Try making:
- A greeting program
- A calculator
- A number guessing game
- A simple grade checker
- A list printer using arrays and loops
Every small project teaches you something new. Keep building, keep testing, and keep asking questions. That is how programmers grow.

