Chapter 1: Introduction to C# and .NET
Version: C# 13 · .NET 9
What Is C#?
C# (say it as "C sharp") is a programming language made by Microsoft. You write instructions in C#, and the computer follows them: it calculates, stores information, shows text on the screen, and much more.
A programming language on its own is not enough. You also need tools that turn your text into something the computer can run, plus a ready-made library of useful code. For C#, that package is called .NET.
So keep this simple picture in your head:
- C# is the language you write.
- .NET is the platform that runs your C# code and gives you ready-made tools.
You will use the two together, every time.
Where Is C# Used?
C# is not a toy language for one small job. Companies use it for real products that people use every day.
Games. The Unity game engine uses C# as its main scripting language. If you write the code that moves a character, counts the score, or opens a door in a Unity game, you write it in C#.
Business software. Banks, hospitals, shops, and offices run large systems written in C#. These programs handle accounts, bills, stock, and salaries. They need to be safe and correct, and C# is built for that kind of work.
Web sites and web APIs. With a part of .NET called ASP.NET Core, you build the server side of web sites. An API is the part that a phone app or a web page talks to in order to get data, for example the list of products in an online shop.
Windows desktop apps. Many everyday Windows programs, such as tools with buttons, forms, and lists, are written in C#.
One language, many kinds of app:
Why Learn C#?
There are good, practical reasons.
First, jobs. Many companies hire C# developers for business software, web APIs, and Unity games. One language opens several doors.
Second, C# is friendly to learn but not limited. The first programs are short and clear. Later, the same language can handle very large projects. You do not need to switch languages as you grow.
Third, the tools are free. The .NET SDK, the set of tools you install on your computer, costs nothing and works on Windows, Mac, and Linux.
NOTE SDK stands for Software Development Kit. In plain words, it is the box of tools that lets you build and run programs.
Install the .NET SDK
Follow these steps once. After this, your computer is ready for the whole book.
- Go to the official .NET download page in your browser.
- Download the .NET 9 SDK for your system: Windows, Mac, or Linux.
- Run the installer and accept the default choices.
Now check that the install worked. Open a terminal (on Windows this can be Terminal or Command Prompt) and type:
dotnet --version You should see a version number that starts with 9, for example:
9.0.100 The exact number after 9.0. does not matter. If you see a number, the SDK is installed and ready.
GOTCHA Install the SDK, not just the "runtime". The runtime can run finished programs, but it cannot build your code. The SDK includes the runtime, so the SDK is the safe choice.
What Else Do You Need?
Only one more thing: a place to write code. You can use any plain text editor, but a code editor makes life easier. It colours the code so the parts stand out, and it warns you about some mistakes while you type. Many C# learners use Visual Studio Code, which is free, or Visual Studio, which is also free in its Community edition. If you do not want to install anything yet, a simple editor is enough for the first chapters: every program in Part 1 is short, and the dotnet tool does the real work of building and running.
What you do not need is a powerful computer. If your machine can run a browser and a terminal, it can run every program in this book.
Create Your First Project
A C# program lives inside a project. A project is a folder that holds your code files and one settings file. The dotnet tool creates the folder for you.
In your terminal, run these two commands, one after the other:
dotnet new console -n HelloApp
cd HelloApp The first command makes a new console project called HelloApp . A console program is the simplest kind: it shows text in the terminal and can read text that you type. The second command moves you inside the new folder.
Inside HelloApp you will find a file called Program.cs . The .cs at the end means "C# source file". This is where your code lives. Open it and you will see one line already written for you.
Your First Program
Replace everything in Program.cs with this program:
Console.WriteLine("Hello, C#!");
Console.WriteLine("My name is Salam."); Now run it from the terminal, from inside the HelloApp folder:
dotnet run Output:
Hello, C#!
My name is Salam. Well done. You just ran your first C# program.
Let us look at the code slowly. Console is the terminal window. WriteLine means "write a line of text". So Console.WriteLine("Hello, C#!"); tells the terminal to show the text Hello, C#! and then move to a new line. Text that your program shows must sit inside double quotes, like "Hello, C#!" . The semicolon ; at the end marks the end of one instruction, the same way a full stop ends a sentence.
The second line does the same thing again with different text, so the two lines appear one below the other.
Change It and Run Again
Programming is learned by changing things. Try this version, which prints three lines:
Console.WriteLine("C# can build:");
Console.WriteLine("- Games with Unity");
Console.WriteLine("- Web APIs with ASP.NET Core");
Console.WriteLine("- Business software for offices"); Output:
C# can build:
- Games with Unity
- Web APIs with ASP.NET Core
- Business software for offices Each WriteLine adds one new line. Run the program after every small change. That habit, change one thing and run it, will save you hours of confusion later.
PRO TIP If the computer shows a red error message, do not panic. Read the first line of the error and check the line number it names. Most beginner errors are a missing semicolon, a missing quote, or a spelling mistake such as Writeline instead of WriteLine . C# cares about capital letters.
Try It Yourself
- Write a program that prints your name on one line and your city on the next line.
Answer
Console.WriteLine("Salam");
Console.WriteLine("Sylhet"); Output:
Salam
Sylhet Use your own name and city. The output shows the two lines you printed.
- What is wrong with this line: Console.WriteLine("Hello!); — and how do you fix it?
Answer The closing double quote is missing, so the text never ends. The fixed line is `Console.WriteLine("Hello!");` and this program runs:
Console.WriteLine("Hello!"); Output:
Hello! - Write a program that prints a small menu of three foods, one food per line, with a title line first.
Answer
Console.WriteLine("Today's menu:");
Console.WriteLine("1. Rice and chicken");
Console.WriteLine("2. Noodles");
Console.WriteLine("3. Tea"); Output:
Today's menu:
1. Rice and chicken
2. Noodles
3. Tea