CodeGym /Courses /C# SELF /Diving into a C# Project

Diving into a C# Project

C# SELF
Level 5, Lesson 4
Available

1. General Structure of a C# Project

Your program isn’t just a single file, but a whole collection of files, folders, and settings. C# is a strongly typed language, and for big apps it’s super important to keep everything organized. If you ignore the project structure, your code will quickly turn into a digital maze where you can’t even find your own "Hello, World!" output line.

Also, understanding project structure is a skill that gets noticed at interviews. It’s like knowing where the keys and TV remote are in your house — life’s just easier!

To make it clear, imagine our C# project is a box with compartments:

MyFirstConsoleApp/
├── MyFirstConsoleApp.csproj
├── Program.cs
├── Properties/
│    └── launchSettings.json
├── bin/
│    └── ... (this is where the compiled program goes)
├── obj/
│    └── ... (this is where intermediate compilation files go)

Structure details:

Name What it’s for
MyFirstConsoleApp.csproj
Project file: the “passport” of your whole app, all project settings are here.
Program.cs
Main file with the entry point (Main). This is usually where you write your first code.
Properties/
Folder with project settings, for example, launch options from the IDE.
bin/
“Basket for finished products” — this is where the built (compiled) executable goes.
obj/
Internal compilation working files (like drafts when writing a novel, you rarely look in here).

2. Project File (.csproj)

The file with the .csproj extension is the heart and brain of the project. All the global settings are written here: language version, dependencies, which files to compile, which packages are connected, etc. Switch to Solution Explorer and go to the Files tab.

Example file for a console app:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project>
  • <TargetFramework>net9.0</TargetFramework> — says the project is targeting .NET 9.
  • <OutputType>Exe</OutputType> — means you’ll get an executable file (.exe for Windows).
  • The <PropertyGroup> block holds the main parameters.
  • In this file you can add extra NuGet packages, change compilation settings, turn features on/off.

3. Main Program File: Program.cs

If you compare the project to a theater, Program.cs is the stage, and all the other files are backstage. This is where the so-called “entry point” of the app lives — the Main method.


// Program.cs
using System;

namespace MyFirstConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, world!");
        }
    }
}
Minimal example of Program.cs
  • namespace — namespace, helps group classes and avoid name conflicts.
  • class Program — main class where the Main method lives.
  • static void Main(string[] args) — the required entry point — this is where the program starts running.
  • Console.WriteLine() — the way you already know to tell the world your thoughts.

Updating our app

Remember in previous lectures we made a mini-app that asked for the user’s name and greeted them? Here’s an example:


// Program.cs
using System;

namespace MyFirstConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter your name: ");
            string name = Console.ReadLine();
            Console.WriteLine($"Welcome, {name}! Live long and prosper.");
        }
    }
}

Run the project: click the green ▶️ Run button.

If everything went well, a Run tab will appear at the bottom. You’ll see the program started and is waiting for your answer to Please enter your name:. Type any name (for example, "Cat") right in this console and hit Enter.

Right after you hit Enter, the program will process your input and print the final message with your name. The program finished its work, and we see the result.

4. How to Write Your Code: Basic Rules

Where to write code?

At first, it’s best to write your main logic in Program.cs inside the Main method. As your app grows, you’ll create new files and classes, but for now don’t get ahead of yourself — all the fun stuff happens right here.

What can (and can’t) you do in Main?

  • You can: declare variables, write calculations, call methods, print/read data.
  • You can’t: write code “outside” methods or classes (for example, just a line like Console.WriteLine("Hello"); outside Main — don’t do that!).

Comments

Comment your code for yourself and others. For single-line comments use //, for multi-line — /* ... */. The easier it is to figure out later why you wrote that mysterious piece of code, the better.

// This is a single-line comment

/*
 And this is —
 a multi-line one.
*/

5. How Does the Program Run?

Your program always starts running from the Main method. Everything before those lines is ignored. Want to do something at startup? Put it in Main.

Example — simplified flowchart (code → run):

Program start
     |
     v
Main method
     |
     v
Run instructions inside Main (in order)
     |
     v
Program end

6. How Your App Grows: Adding New Files

When should you create a new file?

When your code doesn’t fit on one screen anymore, and you can’t keep all the variables in your head, it’s time to split your code into parts.

Example: if you’re making a calculator, you can make a separate Calculator.cs file and move a separate class there.

MyFirstConsoleApp/
├── Program.cs
├── Calculator.cs  // New file with calculator logic

How to add a new file in Rider

  1. In the Solution Explorer tree, right-click the project → "Add" → "New File" (or "New Class").
  2. Give the file a meaningful name.
  3. Write a new class or function in the file.
// Calculator.cs
namespace MyFirstConsoleApp
{
    class Calculator
    {
        public static int Add(int a, int b)
        {
            return a + b;
        }
    }
}

Now let’s use it from Program.cs:

// Program.cs
using System;

namespace MyFirstConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = Calculator.Add(2, 3);        //Using Add function from Calculator class
            Console.WriteLine($"2 + 3 = {sum}");
        }
    }
}

7. "Build" and "Run" — What’s Under the Hood?

When you hit Run (or F5), the IDE:

  • Checks the syntax and builds your code into a binary file (.exe or .dll) in the bin/Debug/net9.0/ or bin/Release/net9.0/ folder.
  • Runs the built file.
  • Prints the result in the terminal/console you see.

bin/ and obj/: what are these folders for?

  • bin/ — this is where your finished product lives (built programs).
  • obj/ — this is where temporary files live, needed by the compiler for intermediate calculations.

Usually you can just ignore these folders, their contents will be rebuilt automatically if needed.

8. Common Mistakes When Writing Code and Working with a Project

Writing code isn’t just banging on the keyboard blindly. As soon as errors like CS5001: Program does not contain a static 'Main' method suitable for an entry point pop up, programmers start googling en masse, and the IDE complains in even more complicated language.

  • One common mistake is code outside a method or class. In C# you can’t just write an expression right in the program file. Everything has to be inside a class and method!
  • Different file and class names. You can do this, but it’s better to give files and classes the same, clear names — for your (and your future coworker’s) convenience.
  • Got lost in namespaces. If you made a class in another namespace, don’t forget to connect it with using.
  • Forgot about the entry point. If you deleted or renamed the Main method, the program won’t run.
2
Task
C# SELF, level 5, lesson 4
Locked
Number of Digits
Number of Digits
2
Task
C# SELF, level 5, lesson 4
Locked
Minimum of Three
Minimum of Three
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION