C# Quick Start: Your First Program

Welcome to the world of C# programming! If you've followed along, you now have a good sense of the .NET ecosystem and how projects are structured. Now, it's time to get your hands dirty and actually write some code. Learning a new programming language can seem like a big step, but trust me, C# is known for its elegance and power, and it's a fantastic language for test automation.

We're going to start with the absolute basics, the traditional first step every programmer takes: making the computer say hello! It's a simple act, but seeing your own code come to life is an incredibly rewarding moment.

So, let's create our version of "Hello, World!" – a warm welcome to you, the "Wanderer" on this coding path. 🥾

Tommy and Gina coding at holographic terminals

What is C# – Your New Superpower

Before we type our first line of code, let's quickly recap what C# (C-Sharp) is. It's a modern, versatile, and very popular programming language developed by Microsoft. It's a cornerstone of the .NET platform, which we learned about in a previous lesson.

Here are some of its key characteristics that make it so powerful and a great choice for us:

  • Object-Oriented: C# is built around the concept of objects, which helps organize code into reusable blueprints called classes. This makes code more modular, easier to understand, and maintainable – all crucial for building robust test automation frameworks. (We'll dive deep into Object-Oriented Programming, or OOP, later!)
  • Type-Safe: This is a big plus! C# checks that you're using data types correctly before your program runs (at compile-time). This helps catch a lot of common errors early on, leading to more reliable code.
  • Component-Oriented: It's designed for building software from self-contained, reusable components (those assemblies we talked about).
  • Rich Libraries: Being part of .NET, C# has access to the enormous Base Class Library (BCL), giving you ready-made tools for countless tasks.
  • Versatile and Widely Used: C# isn't just for one thing. It's used to build high-performance web applications, powerful desktop software, popular games (especially with the Unity engine), cloud services, mobile apps, and importantly for our journey, sophisticated test automation solutions.

Think of C# as your multi-tool – powerful, adaptable, and capable of building almost anything you can imagine within the .NET universe.

Setting Up Your Coding Cockpit

Before you can write and run your amazing C# programs (and eventually, your test automation scripts!), you need to set up your development environment. Think of this as preparing your workshop with the essential tools. To start writing and running C# .NET applications, you need two main things:

  1. .NET SDK (Software Development Kit): This is non-negotiable. It's the foundation for all .NET development and includes:
    • The .NET Runtime (which actually runs your compiled code).
    • The .NET Libraries (like the Base Class Library we talked about, full of useful pre-built code).
    • The dotnet Command-Line Interface (CLI) tool – a powerful command-line utility for creating, building, running, and managing .NET projects.
  2. An IDE (Integrated Development Environment) or a Code Editor: This is the software where you'll actually write, edit, and often debug your C# code.

Let's get the SDK installed first.

Installing the .NET SDK

Follow these steps to get the .NET SDK onto your system:

  1. Download the SDK:
  2. Choose Your Version:
    • On the download page, you'll see options for different .NET versions. You should download the .NET SDK – make sure it's the SDK and not just the Runtime.
    • I recommend downloading the latest stable LTS (Long-Term Support) version available (for example, .NET 8.0 was an LTS version around early 2024). LTS versions provide stability and are supported by Microsoft for a longer period. Alternatively, you can choose the latest ST (Standard Term) version if you want access to the newest features, but be aware its support window is shorter.
  3. Install the SDK:
    • Once downloaded, run the installer. It's a straightforward process; just follow the on-screen instructions. It works on Windows, macOS, and various Linux distributions.
  4. Verify Your Installation:
    • After the installation is complete, it's a good idea to verify that it worked correctly. Open a terminal or command prompt:
      • Windows: Search for cmd (Command Prompt) or PowerShell in the Start Menu.
      • macOS: Open Terminal (you can find it via Spotlight search or in Applications > Utilities).
      • Linux: Open your preferred terminal application.
    • In the terminal window, type the following command and press Enter:
      dotnet --version
    • You should see a version number printed to the screen, something like 8.0.100 or similar (the exact numbers will depend on the version you installed). If you see a version number, congratulations, your .NET SDK is installed correctly! 🎉

Choosing Your Code Editor or IDE

With the .NET SDK installed, you now need a place to write your C# code. The main options we discussed earlier are:

  • Visual Studio: Microsoft's flagship IDE. The free Visual Studio Community Edition is incredibly powerful, packed with features, and an excellent choice for .NET development. It provides a rich environment for coding, debugging, managing projects, and interacting with NuGet packages.
  • Visual Studio Code: A free, lightweight, yet very powerful and popular source code editor that runs on Windows, macOS, and Linux. To make it great for C# development, you'll want to install the official C# Dev Kit extension from Microsoft (available in the VS Code Marketplace). Many developers prefer VS Code for its speed and flexibility.

You can use the links above to download and install either Visual Studio IDE or Visual Studio Code. Alternatively, check out the official Microsoft tutorial linked in the Start Your C# Adventure section at the end of this lesson.

If you're just starting, Visual Studio Community might offer a slightly more guided experience out of the box, but VS Code with the C# Dev Kit is equally capable. For our very first program, which will be a Console Application, either of these tools will work perfectly.

Console Application: A program that interacts with the user primarily through a text-based interface (the console or terminal), typically used for utility programs or for learning programming fundamentals because they avoid the complexity of graphical user interfaces (GUIs).

Once you have the .NET SDK and your chosen editor/IDE installed, you're all set to write your first C# code!

Your First C# Program

Alright, the moment of truth! Let's write the code that will make your computer greet you. This is the traditional "Hello, World!" program, but with our own little twist.

// This is our very first C# program!
// It will print a welcome message to the console.
 
using System; // 1. Tells our program we want to use things from the 'System' namespace
 
namespace HelloWandererApp // 2. Organizes our code into a custom 'namespace'
{
    class Program // 3. Defines a 'class' named Program - our code lives in classes
    {
        // 4. This is the Main method - the starting point of our program
        static void Main(string[] args)
        {
            // 5. This line writes our message to the console window
            Console.WriteLine("Hello, Wanderer! Welcome to C#.");
 
            // 6. This line will keep the console window open until you press Enter
            Console.ReadLine();
        }
    }
}    

Let's break down this masterpiece, line by line (well, section by section):

  1. using System;
    • The using keyword is like saying, "Hey C#, I want to use some tools from a specific toolbox." In this case, the System toolbox (which is a namespace) contains many fundamental tools, including the Console tool we need for printing.
  2. namespace HelloWandererApp { ... }
    • As we learned in the previous lesson, namespaces help organize our code and prevent naming conflicts. Here, we're creating our own namespace called HelloWandererApp to hold our program's code.
  3. class Program { ... }
    • In C#, most of your code will live inside a class. Think of a class as a blueprint for creating objects (more on that much later!). For a simple console app, we often have a default class named Program.
  4. static void Main(string[] args) { ... }
    • This is the most important part for a console app – it's the entry point. When you run your program, the .NET runtime looks for this specific Main method and starts executing code from here.
    • static: Means this method belongs to the Program class itself, and you don't need to create an "instance" of the class to run it. (Don't worry too much about static for now).
    • void: Means the Main method doesn't return any value when it finishes.
    • string[] args: This allows your program to receive arguments from the command line when it's run. We'll ignore args for our simple program.
  5. Console.WriteLine("Hello, Wanderer! Welcome to C#.");
    • Console: This is a built-in class (from the System namespace) that provides methods for interacting with the console window.
    • .WriteLine(): This is a method (a function that belongs to a class) of the Console class. Its job is to write a line of text to the console.
    • "Hello, Wanderer! Welcome to C#.": This is a string – a sequence of characters enclosed in double quotes – that we want to display.
    • ; (Semicolon): Super important! In C#, most statements must end with a semicolon. It tells the compiler that you've finished that particular instruction. Forgetting it is a very common beginner mistake.
  6. Console.ReadLine();
    • It tells the program to wait for the user to press the Enter key before closing the console window. This can be useful when you're running your program from Visual Studio, as sometimes the console window closes very quickly after printing the message.

That's it! Not too scary, right? Just a few key pieces working together.

Hello World! C# for Beginners

Bringing Your Code to Life – Compile & Run

Writing the code is one thing; seeing it run is where the magic happens! Here's conceptually how you'd do it:

Using Visual Studio (e.g., Community Edition):

  1. Create a New Project: Go to File > New > Project....
  2. Choose the Template: Look for "Console App". Make sure it's for C# and the modern .NET (e.g., .NET 8 or similar, not .NET Framework unless you have a specific reason). Give your project a name, like HelloWorld.
  3. Write Your Code: Visual Studio will generate a Program.cs file, often with some basic "Hello, World!" code. You can replace the contents of the Main method (or the whole file if you prefer) with our "Hello, Wanderer!" code snippet above.
  4. Run the Program:
    • Look for a "Start" button (often a green play triangle ▶️) in the toolbar and click it.
    • Alternatively, you can usually press the F5 key.
  5. Behold! A console window should appear, display your message: "Hello, Wanderer! Welcome to C#!", and then it might close quickly (unless you added Console.ReadLine();).

Using the .NET CLI (Command Line Interface):

For those who like the command line, the .NET CLI is your friend.

  1. Open your terminal or command prompt.
  2. Navigate to where you want to create your project.
  3. Run dotnet new console -o HelloWandererCLI (this creates a new console project in a folder named HelloWandererCLI).
  4. Navigate into the project folder: cd HelloWandererCLI.
  5. Open the Program.cs file in your favorite text editor (like VS Code) and paste in our "Hello, Wanderer!" code.
  6. Back in the terminal, in your project folder, run: dotnet run.
  7. You should see your message printed directly in the terminal!

Don't Fear Red Squiggles!

As you start typing code in Visual Studio or VS Code (with the C# extension), you might see red (or sometimes green) squiggly lines appearing under parts of your code. These are your friends, not your enemies! They are the IDE's way of telling you about syntax errors (like a missing semicolon) or potential issues before you even try to run your program. Hover over them with your mouse; a tooltip will usually pop up explaining the error. Learning to read and understand these messages is a huge part of becoming a programmer. It's all part of the learning process!

The feeling of writing code and seeing it execute your instructions is a fantastic motivator. That simple "Hello, World!" is your first successful step into the world of C# programming!

Key Takeaways

  • C# is a modern, versatile, object-oriented programming language within the .NET ecosystem, ideal for test automation.
  • A basic C# console application typically includes using directives for namespaces, a custom namespace for organization, a class (like Program), and a static void Main(string[] args) method which is the program's starting point.
  • The Console.WriteLine("Your message here"); statement is used to print text to the console window. Remember that semicolon!
  • You can create, compile, and run C# console applications using Visual Studio or the command-line .NET CLI (dotnet run).
  • Successfully writing and running your first "Hello, World!" program is a significant milestone – celebrate it! 🎉

Start Your C# Adventure

What's Next?

Congratulations on writing your first C# program! That 'Hello, Wanderer!' is just the beginning. Now that you've seen a program run, let's start exploring the fundamental building blocks of C#: Data Types, Variables, and Operators. This is where the real power starts to unfold!