Skip to main content

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

  1. Open Visual Studio 2022 (download from visualstudio.microsoft.com if needed)
  2. Click File > New > Project
  3. In the search box, type "Console App"
  4. Select Console App (the one that says C# - not C++ or VB)
  5. Click Next
  6. Configure your project:
    • Project name: HelloCSharp
    • Location: Choose a folder like C:\learning\csharp
    • Check Place solution and project in the same directory
  7. Click Next
  8. Select .NET 8.0 (or the latest version) as the framework
  9. 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

  1. Open VS Code (download from code.visualstudio.com if needed)
  2. Install the C# Dev Kit extension:
    • Click the Extensions icon (or press Ctrl+Shift+X)
    • Search for "C# Dev Kit"
    • Click Install
  3. Open the integrated terminal:
    • Click Terminal > New Terminal
    • Or press Ctrl+`
  4. Create your project folder and navigate to it:
    mkdir C:\learning\csharp
    cd C:\learning\csharp
  5. Create a new console application:
    dotnet new console -n HelloCSharp
    cd HelloCSharp
  6. Open the project:
    • Click File > Open Folder
    • Select the HelloCSharp folder
    • 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:

  1. using System; - Imports the System namespace (gives us access to Console)
  2. class Program - Defines a class named Program (everything in C# lives in classes)
  3. static void Main() - The entry point method where your program starts
  4. Console.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

  1. Open the terminal (Ctrl+`)
  2. Make sure you're in the project folder
  3. Run the command:
    dotnet run
  4. 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:

  1. Print your name
  2. Print your favorite programming language
  3. Print the current year

Steps:

  1. Open Program.cs
  2. 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}");
}
}
  1. Run the program (F5 in VS or dotnet run in 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 string
  • string - A data type for text
  • int.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

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 restore in terminal

Challenge

Create a simple calculator that:

  1. Asks the user for two numbers
  2. Adds them together
  3. 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#!

Additional Resources