Introduction to C# Basics
Welcome to the first chapter of the C# course! In this chapter, you’ll learn the basics of C#, a programming language used all around the world. Before you start writing code, it’s important to understand a few basic ideas that will help you as you learn step by step how to create programs.
What is C#?
C# (pronounced “C-Sharp”) is a programming language created by Microsoft. It’s designed to be easy to learn but powerful enough to create all kinds of software, like apps, games, and programs for computers.
Why should you learn C#?
- Versatile: With C#, you can create all kinds of things, like apps for Windows, websites, games (such as those made with Unity), and even mobile apps.
- Future-proof: C# keeps getting updated with new features, so you’ll always be learning the latest technology.
- In-demand: Many companies are looking for people who know how to program in C#, so it’s a great skill to have!
What will you learn?
In this chapter, we’ll start with the basics. You’ll learn:
- How to write your first program, the famous “Hello, World!” program.
- How to use variables and data types to store information like numbers and text.
- How to make decisions in your program using if-statements and how to repeat things with loops.
- How to create functions to organize and reuse parts of your code.
What do you need to get started?
To write and run C# code, you’ll need a special place to type and test your code. Here are two options:
- Visual Studio: This is a program you can download onto your computer. It’s free and has all the tools you need to work with C#.
- How to use the built-in environment:
- Navigate to the coding prompt section below.
- Write your C# code in the input field.
- Click “Run” to execute the code.
- The output will be displayed right below the input field.
This environment works just like any other C# development tool, so you can practice writing and testing your code in real time!
Let’s Start: Understanding Your First C# Code
Now that you’re ready to begin, let’s take a look at your first C# program, the famous “Hello, World!” program. Here’s what the code looks like:
What does this code do?
This program tells the computer to print the message “Hello, World!” on the screen. Let’s break it down step by step:
using System;
This line is like giving the program a toolbox to use. The toolbox is called “System,” and it contains many helpful tools (like showing text on the screen). By writing this, you’re saying, “I need to use some tools from the System toolbox!”class Program
A class is like a blueprint for the program you’re building. In this case, the blueprint is calledProgram
. Inside this blueprint, you tell the computer exactly what you want it to do. Every C# program has at least one class.static void Main()
This part is very important! Main is the place where the computer starts reading your instructions. Think of it like the starting point of your program. Whenever you run the program, the computer will start by looking at the code inside the Main method.Console.WriteLine("Hello, World!");
This line is the command that makes your program actually print something to the screen.Console
is a tool from the System toolbox. It allows you to show messages on the screen.WriteLine()
is the action that tells the computer to print whatever is inside the parentheses to the screen."Hello, World!"
is the message we want to display, and it’s written inside quotes because it’s text.
So, when you run this code, the computer follows these steps and prints “Hello, World!” to the screen. It’s a simple way to check that everything is working.
Step 2: Variables and Data Types
What is a variable?
A variable is like a box where you can store information, such as a number or a piece of text. Once you’ve stored something in a variable, you can use it later in your program.
For example, if you’re making a game and you want to keep track of the player’s name, you would use a variable to store that name.
What is a data type?
A data type tells the computer what kind of information is inside a variable. For example, you use one data type for numbers and a different one for text.
Example: Storing a name
Here’s how you can use a variable to store a player’s name:
string playerName = "Alice";
string
: This data type is used to store text (like a name).playerName
: This is the name of the variable (it’s like labeling the box)."Alice"
: This is the value stored inside the variable (the player’s name).
Example: Storing an age
If you want to store a number, such as the player’s age, you would use the int
data type, which stands for “integer” (a whole number):
int playerAge = 12;
int
: This data type is used for whole numbers.playerAge
: This is the name of the variable.12
: This is the value stored inside the variable (the player’s age).
Try it yourself!
Here’s a code example you can run to see how variables work:
using System;
class Program
{
static void Main()
{
string playerName = “Alice”;
int playerAge = 12;
Console.WriteLine(“Player Name: ” + playerName);
Console.WriteLine(“Player Age: ” + playerAge);
}
}
What happens?
- The program creates two variables:
playerName
andplayerAge
. - Then, it prints the player’s name and age on the screen.
Assignment!
Make it your name!
And your age!
Like i did.