Skip to main content

Command Palette

Search for a command to run...

🧮 Building a C# Calculator with Git & GitHub: A Hands-On Guide

A step-by-step guide to version control, branching strategies, and collaborative development using VS Code and WSL

Updated
9 min read
🧮 Building a C# Calculator with Git & GitHub: A Hands-On Guide

Introduction

Mastering version control is what separates coders from professional developers. While many tutorials teach C# syntax, few bridge the critical gap to real-world collaboration.

This guide solves that. We'll build a C# calculator not just as a coding exercise, but as a practical journey through essential Git workflows. You'll learn branching, merging, conflict resolution, and GitHub integration—precisely the skills teams expect you to know.

Through hands-on practice in VS Code with WSL, you'll transform from writing isolated code to managing professional version control workflows. Let's begin.

Prerequisites: Setup Checklist

Before we begin, ensure you have the following tools installed and configured. This setup creates a professional development environment that mirrors industry standards.

  • Windows Subsystem for Linux (WSL): A compatibility layer for running Linux binary executables natively on Windows.

    • How to get it: Open PowerShell as Admin and run wsl --install. Restart if prompted.
  • Visual Studio Code: A lightweight, powerful source code editor.

  • Remote - WSL Extension: Essential for using VS Code with your WSL terminal.

    • How to get it: Install from the VS Code Marketplace or run code --install-extension ms-vscode-remote.remote-wsl.
  • .NET SDK: The Software Development Kit for building C# and .NET applications.

    • How to verify: In your WSL terminal, run dotnet --version. You should see a version number (e.g., 8.0.101).
  • A GitHub Account: The platform for hosting and collaborating on code.

    • How to get it: Create a free account at github.com.

Pro Tip: Configure Git in WSL with your identity before starting:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

📁 Step 1: Laying the Foundation – Project Setup

First, we need to create our project home and its basic structure.

Action: In your Ubuntu terminal, run these commands:

# 1. Create a new directory for our project
mkdir CSharpCalculator

# 2. Navigate into that new directory
cd CSharpCalculator

# 3. Use the .NET command to create a new "Console Application" project.
# This automatically generates the necessary starter files for us.
dotnet new console

# 4. Open this entire project folder in VS Code
code .

What Happens? The dotnet new console command created two key files for you:

  • CSharpCalculator.csproj: The project configuration file.

  • Program.cs: The main file where we'll write our C# code.

You now have a working (but very basic) C# application. You can run it with dotnet run in the terminal.


📄 Step 2: Taking Snapshots – Initializing Git

Now, let's turn this folder into a Git repository. Think of Git as a super-powered "Save Game" feature for your code.

Action: In your terminal, make sure you're still in the CSharpCalculator directory and run:

# 1. Initialize a new Git repository here
git init

# 2. Add all the files in the current directory to Git's "staging area"
# This tells Git, "Hey, start tracking these files!"
git add .

# 3. Take your first "snapshot" (called a commit) of the project
git commit -m "Initial commit: Basic console app structure"

Congratulations! Your code is now being tracked by Git. The -m flag lets you add a message describing what this snapshot contains. Make these messages clear and descriptive—your future self will thank you.


🌿 Step 3: Building in Isolation – Feature Branches

Building everything in one place gets messy. Instead, we'll build each feature on its own separate branch. A branch is an independent line of development. It's like working on a copy of your project without messing up the original.

Let's build our calculator functions one by one.

🔹 Branch 1: The Addition Feature

Action:

# Create a new branch named 'add-feature' and switch to it
git checkout -b add-feature

Now, open Program.cs in VS Code, delete all the existing code, and replace it with this:

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Addition: 5 + 3 = " + Add(5, 3));
    }

    static int Add(int a, int b) => a + b;
}

Save the file. Now, commit this change to the add-feature branch:

git add Program.cs
git commit -m "feat: add addition function"

🔹 Repeat for Other Features

Follow the same pattern for the other operations. For each one:

  1. Go back to the clean slate of the main branch: git checkout main

  2. Create and switch to a new branch: git checkout -b subtract-feature

  3. Replace the code in Program.cs with the new function.

  4. Commit the change.

Code for Subtraction:

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Subtraction: 10 - 4 = " + Subtract(10, 4));
    }

    static int Subtract(int a, int b) => a - b;
}

Code for Multiplication:

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Multiplication: 6 * 7 = " + Multiply(6, 7));
    }

    static int Multiply(int a, int b) => a * b;
}

Code for Division:

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Division: 20 / 5 = " + Divide(20, 5));
    }

    static double Divide(double a, double b) => a / b;
}

Pro Tip: Use git branch to see the list of all your branches. You're building a powerful set of features in perfect isolation!


⚔️ Step 4: The Rite of Passage – Resolving a Merge Conflict

Conflict is a normal part of collaboration in Git. It happens when two branches change the same line of the same file. Let's cause one on purpose and fix it. It's easier than it sounds!

Action:

# 1. Switch back to the main branch
git checkout main

# 2. Merge the add-feature branch into main. This will work fine.
git merge add-feature

# 3. Now, try to merge the subtract-feature branch.
git merge subtract-feature

BAM! CONFLICT! Git will tell you there's a conflict in Program.cs. Don't panic!

Fixing the Conflict in VS Code:

  1. Open Program.cs. You'll see something messy with <<<<<<<, =======, and >>>>>>>.

  2. These markers show the two conflicting changes. Your job is to edit the file to what you want the final code to be.

  3. A good resolution is to keep both function calls. Delete the conflict markers and make your Main method look like this:

    static void Main(string[] args)
    {
        Console.WriteLine("Addition: 5 + 3 = " + Add(5, 3));
        Console.WriteLine("Subtraction: 10 - 4 = " + Subtract(10, 4));
    }
  1. Save the file.

Finalizing the Merge:

# Tell Git you've fixed the conflict by adding the file
git add Program.cs

# Commit to complete the merge
git commit -m "Merge subtract-feature and resolve conflict in Main()"

You did it! You've successfully navigated a merge conflict. This skill is absolute gold on a development team.


☁️ Step 5: Going Public – Pushing to GitHub

It's time to share our work with the world by pushing it to GitHub.

Action:

  1. Go to github.com and create a new repository. Name it CSharpCalculator. Do not initialize it with a README.

  2. On the new repo's quick setup page, copy the commands for "push an existing repository."

  3. Run them in your terminal:

# Connect your local repo to GitHub (replace YOUR_USERNAME with yours e.g CSharpCalculator)
git remote add origin https://github.com/YOUR_USERNAME/CSharpCalculator.git

# Rename the default branch to 'main' (if needed)
git branch -M main

# Push your code up to GitHub
git push -u origin main

4. Push Your Feature Branches: Now, let's push each feature branch so they appear on GitHub too.

git push origin add-feature
git push origin subtract-feature
# ... and so on for your other branches

🔁 Step 6: Collaboration Simulation – Pull Requests

On GitHub, you'll now see your branches. This is where the collaboration magic happens.

  1. Go to your GitHub repo's main page.

  2. You'll likely see a notification for your recently pushed branches (e.g., add-feature). Click "Compare & pull request".

  3. A Pull Request (PR) is a proposal to merge changes from one branch into another. Add a title and description like "Adding the core addition feature."

  4. Click "Create pull request".

  5. You can now review your own changes, add comments as if you were a teammate, and finally, click "Merge pull request".

Repeat this process for your other branches. This workflow is the standard for code review in modern software development.


🧪 Step 7: Trust, but Verify – Adding Unit Tests

Good code is tested code. Let's add a unit test project to ensure our math is always correct.

Action: In your terminal, from the root CSharpCalculator folder:

# 1. Create a directory for our tests
mkdir CalculatorTests

# 2. Navigate into it
cd CalculatorTests

# 3. Create a new xUnit test project
dotnet new xunit

# 4. Link the test project to our main calculator project
dotnet add reference ../CSharpCalculator.csproj

Now, in VS Code, open the new file CalculatorTests/UnitTest1.cs. Replace its code with a test for our Add function:

using Xunit;

public class CalculatorTests
{
    [Fact]
    public void Add_ReturnsCorrectSum()
    {
        // Arrange & Act - We are testing the Add method from Program.cs
        var result = Program.Add(5, 3);

        // Assert
        Assert.Equal(8, result);
    }
}

📘 Step 8: Show Your Work – The README.md

Every great GitHub repo has a great README. It's the front page of your project.

Action:

# From the project root, create a README file
touch README.md

Open README.md in VS Code and add some Markdown to describe your project:

# 🧮 C# Calculator with Git Workflow

A simple calculator built in C# to demonstrate core Git and GitHub workflows, including branching, merging, and pull requests.

## Features
- **Addition**
- **Subtraction**
- **Multiplication**
- **Division**
- Unit Tests with xUnit

## How to Run
1. Clone the repo
2. Run `dotnet run` in the `CSharpCalculator` directory.
3. Run tests with `dotnet test` in the `CalculatorTests` directory.

## Why This Project?
This project is a practical exercise in version control and collaborative development, essential skills for any developer.

Add, commit, and push your new README and tests to complete your project!

git add README.md CalculatorTests/
git commit -m "docs: add README and feat: add unit test project"
git push origin main

✅ Conclusion

Look at everything you've accomplished! You didn't just build a calculator; you built it like a professional developer.

You've practiced:

  • Project Setup with the .NET CLI.

  • Version Control by initializing Git and making commits.

  • Branching Strategies by developing features in isolation.

  • Conflict Resolution by merging branches and fixing clashes.

  • Collaboration by pushing code to GitHub and using Pull Requests.

  • Code Quality by writing unit tests and documentation.

This project is a fantastic addition to your portfolio. It demonstrates you not only can write code, but you understand the process of building software. Keep experimenting, keep learning, and happy coding

More from this blog

Joel Thompson

19 posts