CodeGym /Courses /C# SELF /Structure of a Typical C# Program

Structure of a Typical C# Program

C# SELF
Level 8, Lesson 1
Available

1. A Program is Like an Office Full of Departments

When you're just starting out with programming, it feels like you can just write code line by line and everything will work. But as soon as your program gets a bit more complicated, your brain starts to get all tangled: where is what, why was this written, and if you need to change a line—where the heck is it?..

That's why C#, like most modern languages, gives us a way to "put code on shelves" so you can always figure out what the program does and quickly find what you need.

Imagine a company. There's HR, accounting, IT, logistics, and so on. Each department has its own responsibilities, employees, and tasks.

In a C# program, each "department" is a class. One class handles users, another does calculations, a third works with files.

Here's what a typical simple project looks like:

  • Program.cs — the main office: this is where the "entry point" is, where everything starts.
  • User.cs — the department where user data is stored and processed.
  • Calculator.cs — the calculations department.
  • FileHelper.cs — the file specialists.

Even in a tiny program, there's usually at least one class. And big projects? That's a whole "skyscraper" of hundreds of classes!

2. A Class is a Toolbox

C# is an object-oriented language and everything in it is an object. The types of objects are called classes.

A class is like a box or a locker where we put the stuff we need: variables, methods, properties. Imagine a box labeled "Hammers": inside are only tools related to hammers.

class Example
{
    // Here will be our tools: variables, methods, properties
}
Declaring a class in C#

Classes usually contain variables and functions. In C#, class variables are called fields, and class functions are called methods. But the idea is the same.

Remember our first code?

Console.WriteLine("Hello everyone!");
string name = Console.ReadLine();

This code has both a class and methods:

  • Console — this is a class that contains methods (functions) for working with the console.
  • WriteLine — this is a method of the Console class that prints text to the screen.
  • ReadLine — this is a method of the Console class that reads input from the keyboard.

All programming is basically about writing your own classes and methods that call methods of other classes.

3. Class Fields (Variables) — Its Memory

Inside a class, you can store variables (often called "fields"). They're like memory cells where we stash some data.

class Counter
{
    int value; // field — this will store the current value of the Counter
}
Example of a class field (variable)

Fields let a class remember stuff between different method calls. For example, counting how many times a button was pressed.

You can have as many fields as you want. Example:

class User
{
    int id;             // unique user number
    string name;        // user name
    int age;            // age
    string address;      // address
    User[] children;    // children
}

4. Methods — Actions a Class Can Do

Methods are the "skills" of our class. If a class is an employee, methods are what they can do.

  • A method can print something to the screen.
  • Calculate the sum of two numbers.
  • Take data "as input" and return a result.

Method structure:

  • Return type (for example, void if it returns nothing, or int if it returns a number)
  • Method name (for example, Print)
  • Parameters in parentheses (can be empty)
  • Curly braces { ... } — method body
class Printer
{
    public void PrintHello()
    {
        Console.WriteLine("Hello!");
    }

    public int Add(int a, int b)
    {
        return a + b;
    }
}
Example of class methods

You'll learn more about methods and all their quirks in the next lectures.

5. Properties — Smart Variables

A property (property) is like a "leveled-up" variable: it doesn't just store a value, but can also do something when you get or set the value.

Real life example: you can't just go and change an employee's salary — accounting will check if everything's right. Same with a property: it can "check" the data:

class Person
{
    private int age;		// class variable (field)

    public int Age			// Age property. stores data in the class variable age.
    {
        get { return age; }	// get method - called when someone tries to get Age 
        set 				// set method - called when someone tries to change Age 
        {
            if (value >= 0)
                age = value;
        }
    }
}

The code in the set method won't let you set a negative age. We'll dig into properties more in the next lectures.

6. Access Modifiers

In C#, you can define which parts of a class are accessible from other classes, and which are just for internal use. That's what access modifiers are for. If you see the keyword public before a field, method, or class name, you can access it from anywhere in the program.

If you see the keyword private before a field, method, or class name, you can only access it from within its own class. Other classes won't see this field or method. It's like they don't even exist.

  • public — visible to everyone. These methods and properties can be used from other classes.
  • private — visible only inside the class itself. These are internal details that shouldn't be available to others.
class Demo
{
    public int OpenValue;     // visible from outside
    private int secretValue;  // visible only inside

    public void ShowOpen() { Console.WriteLine(OpenValue); }
    private void HideSecret() { Console.WriteLine(secretValue); }
}

Golden rule: fields are usually private, and only the needed methods and properties are made public.

7. What a Real Class Looks Like

Here's a "put together" example — a mini-calculator:

class Calculator
{
    private int result; // field for storing the result

    public int Result
    {
        get { return result; }
        private set { result = value; } // only accessible inside the class
    }

    public void Add(int a, int b)
    {
        result = a + b;
    }

    public void Clear()
    {
        result = 0;
    }
}

This approach is used in pretty much all modern C# programs!

8. Top-level statements

Actually, in C# all code is always inside classes. A class is the smallest unit of a program. There can be lots of classes, but at least one.

Wait a sec, what about all that code we just wrote, there weren't any classes?!

That's one of the trendy features of C#: you can write code without a class, and the C# compiler will turn it into a class for you.

Say you have this code in Program.cs:

// your code
WriteSmile();
WriteSmile();
WriteSmile();

//your own function
void WriteSmile()
{
    Console.WriteLine(":)");
}
Top-level statement: code without a class

The C# compiler will turn it into this:

using System;

class Program
{
    static void Main()			// your code
    {
        WriteSmile();
        WriteSmile();
        WriteSmile();        
    }

    static void WriteSmile()	//your own function
    {
        Console.WriteLine(":)");
    }
}
How the compiler turns a top-level statement into a class

If you only got half of this lecture, no worries: the main thing is you got the gist. In the next lectures, we'll break down everything here step by step.

2
Task
C# SELF, level 8, lesson 1
Locked
Creating a simple class with a field and a method
Creating a simple class with a field and a method
2
Task
C# SELF, level 8, lesson 1
Locked
A class with several methods
A class with several methods
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION