Day 1: Getting Started with C#
Introduction to C#
C# (pronounced "C-Sharp") is a modern, object-oriented programming language developed by Microsoft. It's part of the .NET ecosystem and is widely used for building web applications, desktop applications, games, and more.
Before we write any code, we need to set up our development environment. You can use either Visual Studio 2022 (full IDE with lots of features) or VS Code (lightweight, fast editor). Both are excellent choices!
Setting Up Your Project
Option 1: Visual Studio 2022
- Open Visual Studio 2022 (download from visualstudio.microsoft.com if needed)
- Click File > New > Project
- In the search box, type "Console App"
- Select Console App (the one that says C# - not C++ or VB)
- Click Next
- Configure your project:
- Project name:
HelloCSharp - Location: Choose a folder like
C:\learning\csharp - Check Place solution and project in the same directory
- Project name:
- Click Next
- Select .NET 8.0 (or the latest version) as the framework
- Click Create
You should now see Solution Explorer on the right with your project, and Program.cs in the main editor.
Option 2: VS Code
- Open VS Code (download from code.visualstudio.com if needed)
- Install the C# Dev Kit extension:
- Click the Extensions icon (or press
Ctrl+Shift+X) - Search for "C# Dev Kit"
- Click Install
- Click the Extensions icon (or press
- Open the integrated terminal:
- Click Terminal > New Terminal
- Or press
Ctrl+`
- Create your project folder and navigate to it:
mkdir C:\learning\csharp
cd C:\learning\csharp - Create a new console application:
dotnet new console -n HelloCSharp
cd HelloCSharp - Open the project:
- Click File > Open Folder
- Select the
HelloCSharpfolder - Click Select Folder
You should now see your project files in the Explorer panel on the left, with Program.cs ready to edit.
Your First C# Program
Look at the Program.cs file. In modern C# (.NET 6+), you'll see very simple code:
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
This is using top-level statements - a simplified syntax. Let's modify it to understand the traditional structure:
Replace the code in Program.cs with:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, C# learner!");
Console.WriteLine("Welcome to your learning journey!");
}
}
Understanding the Code
Line by line breakdown:
using System;- Imports the System namespace (gives us access to Console)class Program- Defines a class named Program (everything in C# lives in classes)static void Main()- The entry point method where your program startsConsole.WriteLine()- Prints text to the console and adds a new line
Running Your Code
In Visual Studio 2022
- With Debugging: Press F5
- A console window appears showing your output
- The window closes when the program ends
- Without Debugging: Press Ctrl+F5
- Console stays open until you press a key
- Better for seeing output!
Tip: Look at the Output window at the bottom to see build messages.
In VS Code
- Open the terminal (
Ctrl+`) - Make sure you're in the project folder
- Run the command:
dotnet run - You'll see the output directly in the terminal:
Hello, C# learner!
Welcome to your learning journey!
Key Concepts
1. Namespaces
The using System; directive imports the System namespace, which contains fundamental classes like Console.
2. Classes
Everything in C# is organized within classes. Program is our class name - it's just a container for our code.
3. Main Method
Main is the entry point of any C# application. When you run your program, this method executes first.
4. Console.WriteLine
This method prints text to the console. The ln means "line" - it adds a new line after the text.
Exercise 1: Personalize Your Program
Task: Modify the program to:
- Print your name
- Print your favorite programming language
- Print the current year
Steps:
- Open Program.cs
- Replace the code with:
using System;
class Program
{
static void Main()
{
Console.WriteLine("My name is: [YOUR NAME]");
Console.WriteLine("Favorite language: C#");
Console.WriteLine($"Current year: {DateTime.Now.Year}");
}
}
- Run the program (F5 in VS or
dotnet runin VS Code)
Note: The $ before the string enables string interpolation - the {DateTime.Now.Year} gets replaced with the actual year!
Exercise 2: User Input
Create a program that asks for user input and greets them by name:
using System;
class Program
{
static void Main()
{
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine($"Hello, {name}! Welcome to C#!");
Console.Write("How old are you? ");
string ageInput = Console.ReadLine();
int age = int.Parse(ageInput);
Console.WriteLine($"In 5 years, you'll be {age + 5} years old!");
}
}
New concepts here:
Console.Write()- Prints without a new line (cursor stays on same line)Console.ReadLine()- Reads user input as a stringstring- A data type for textint.Parse()- Converts text to a number
Troubleshooting
Visual Studio Issues
Problem: Console window closes immediately
- Solution: Use Ctrl+F5 instead of F5
Problem: Can't find "Console App" template
- Solution: Go to Tools > Get Tools and Features, ensure ".NET desktop development" is installed
VS Code Issues
Problem: dotnet command not recognized
- Solution: Install .NET SDK from dotnet.microsoft.com/download
Problem: No IntelliSense (auto-completion)
- Solution: Install the C# Dev Kit extension and reload VS Code
Problem: Red squiggly lines everywhere
- Solution: Wait a few seconds for the extension to load, or run
dotnet restorein terminal
Challenge
Create a simple calculator that:
- Asks the user for two numbers
- Adds them together
- Displays the result
Hint: You'll need to use Console.ReadLine() and int.Parse() twice!
Debugging Tips
Visual Studio
- Click in the gray margin next to a line number to set a breakpoint (red dot)
- Press F5 to start debugging
- When code stops at breakpoint, hover over variables to see their values
- Press F10 to step to the next line
VS Code
- Click in the margin to set a breakpoint
- Press F5 and select "C#" when prompted
- Use the debug toolbar to step through code
Next Steps
In the next lesson, we'll dive deep into Variables and Data Types - learning how to store and work with different kinds of data in C#!