1. General Structure of a Class Declaration
So, imagine: a class in C# is like a blueprint for a building. In the blueprint, we describe how many rooms, what color the walls are, where the doors and windows are. In a class, we describe what properties an object has (like name, age) and what it can do (like "say hello", "ask for a salary").
A class declaration always has three main blocks:
- Access modifier (public, private, internal, protected) — who can use this class. To keep it simple, we'll use public for now, so the class is "visible" everywhere.
- The class keyword — so the compiler knows we're really describing a class.
- ClassName — with a capital letter, by convention.
- The class body in curly braces — here we describe everything our class consists of: fields, properties, methods.
In a nutshell, it looks like this:
public class ClassName
{
// Fields, properties, and methods of the class go here
}
Example: Student class
public class Student
{
// Fields (variables that belong to the object)
public string Name;
public int Age;
}
Right now this class is like an empty box: we described that a student should have a name and age, but we haven't taught it to do anything yet.
2. Class Fields
Fields are variables that belong to an object of the class. For example, every student should have their own name and age — so they're described as fields:
public class Student
{
public string Name;
public int Age;
}
Now, when you create an object of type Student, each object will have its own name and age.
Example of creating an object:
Student alex = new Student();
alex.Name = "Alex";
alex.Age = 20;
Here we created an object alex, and then separately assigned values to its fields. This approach is used for the simplest start (we'll talk about constructors in detail in the next lecture).
3. Class Methods (functions)
Methods are the "verbs" of an object, meaning its behavior. The actions it can perform. For example, a student can say hello. For this, you describe a method inside the class:
public class Student
{
public string Name;
public int Age;
public void SayHello()
{
Console.WriteLine($"Hi, my name is {Name}, I'm {Age} years old!");
}
}
Now every student can "say hello" — and will use their own name and age.
Example of using a method:
Student alex = new Student();
alex.Name = "Alex";
alex.Age = 20;
alex.SayHello(); // Will print: Hi, my name is Alex, I'm 20 years old!
4. Class Properties
Properties are "smart" fields, where you can set special actions when reading or writing a value. In modern C#, properties are used more often than fields, because they give you more control and flexibility. It's like electricity: seems simple, but there are tons of possibilities.
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
Here get; set; creates an automatic field, but lets you add logic later when the value changes. For users of the class, properties look almost the same as fields.
Example:
Student kate = new Student();
kate.Name = "Kate";
kate.Age = 18;
Console.WriteLine(kate.Name); // Kate
More about class properties — in the next lectures :P
5. Comments
Boring but important topic! Just like in the rest of your code, you can and should write comments inside a class to explain why you need a certain field or method:
public class Student
{
// Student's name
public string Name { get; set; }
// Student's age (in years)
public int Age { get; set; }
// Student says hello
public void SayHello()
{
Console.WriteLine($"Hi, my name is {Name}, I'm {Age} years old!");
}
}
This makes your code more readable and helps you not get lost even in a big project.
6. Access Modifiers
Access modifiers are kinda like doors with different locks. They define who and from where can access class members. The main modifiers:
- public: available everywhere (our default case).
- private: available only inside this class.
- internal: available within the current assembly (projects, libraries).
- protected: available inside the class and its descendants.
Example for a field that's hidden from the outside world:
public class Student
{
private int age; // Only inside Student
public string Name { get; set; }
public void SetAge(int value)
{
if (value > 0)
age = value;
}
public int GetAge()
{
return age;
}
}
Early on, people often declare all members as public to avoid confusion, but in real projects that's bad practice: some implementation details are better hidden.
7. Static Class Members
Sometimes a class has data or methods that belong not to a specific object, but to the whole class. These are static members (static members).
For example: we want to keep a general counter of students.
public class Student
{
public static int StudentsCount = 0;
public string Name { get; set; }
public int Age { get; set; }
}
You access a static member through the class name:
Student.StudentsCount = 0;
Each object has its own properties/fields, but static ones are shared by everyone. In the example above, if we have 10 students, each has their own Name, but StudentsCount is one for all.
8. Example: Full Student Class (with methods)
Let's put it all together and create a student class that can do everything and is properly set up:
public class Student
{
// Static student counter - applies to all students
public static int Count = 0;
// Name and age - properties
public string Name { get; set; }
public int Age { get; set; }
// Constructor (more about constructors — next lecture)
public Student(string name, int age)
{
Name = name;
Age = age;
Count++; // Every time we create an object, increase the counter
}
// Method for greeting
public void SayHello()
{
Console.WriteLine($"Hi! I'm {Name}. I'm {Age} years old.");
}
}
Usage in the main app:
class Program
{
static void Main()
{
Student alex = new Student("Alex", 20);
Student kate = new Student("Kate", 22);
alex.SayHello();
kate.SayHello();
Console.WriteLine($"Total students: {Student.Count}");
}
}
9. Code Organization: one file = one class
In .NET, it's common to put each public class in a separate file named after the class. For example:
Student.cs
|
|__ public class Student
And in the main program file (Program.cs) — the entry point and main code. This makes it easier to navigate the project, especially when you have a lot of classes.
10. Typical Mistakes When Working with Classes and Objects
Mistake #1: forgot curly braces or put them in the wrong place.
The compiler will throw an error right away, but it's not always clear what's wrong. Especially if the braces are misplaced in a long construct — finding where it broke can be tough.
Mistake #2: declared two classes with the same name in one namespace.
The compiler won't like this kind of "creativity". And that's good — otherwise you'd never figure out which class you're referring to.
Mistake #3: accidentally gave a field the same name as a property.
This creates confusion when accessing class members. Even experienced devs sometimes facepalm: "Am I accessing the field or the property?"
Mistake #4: didn't specify an access modifier.
By default, the class becomes internal, and if you're writing a library — this can unexpectedly limit access to the class from outside.
Mistake #5: accessing static members through an instance.
If you write student.Count instead of Student.Count, the compiler might not forbid it, but it's confusing. It's like you're saying Count belongs to a specific student, but actually — it's for everyone.
GO TO FULL VERSION