1. Object Fields: where the object keeps its data
In C# (and honestly, in most programming languages) people often say: “everything is an object”. An object is a “virtual thing” in your computer’s memory that can store something (data) and can do stuff (methods). In other words, an object has state (fields, data) and behavior (methods).
Examples of objects:
- A cat in a game (Cat)
- A website user (User)
- A book in a library (Book)
- A car in a parking lot (Car)
When you create an object, it’s like you’re making a copy of a template (a class), and this copy can work with its own data.
Fields are variables that “live inside” the object and store its personal data.
Real-life example
Every cat has a name, age, and color. These are personal — every cat has its own.
public class Cat
{
public string Name; // Cat's name — object field
public int Age; // Cat's age — object field
public string Color; // Cat's color — object field
}
When we create objects:
Cat cat1 = new Cat();
cat1.Name = "Barsik";
cat1.Age = 2;
cat1.Color = "gray";
Cat cat2 = new Cat();
cat2.Name = "Murka";
cat2.Age = 5;
cat2.Color = "black";
Each object has its own field values!
| Object | Name | Age | Color |
|---|---|---|---|
| cat1 | Barsik | 2 | gray |
| cat2 | Murka | 5 | black |
A field is an internal “pocket” in the object where it can stash something.
2. Object Methods: what the object can do
A method is an “action”, “skill”, or “function” of the object. A method is a chunk of code you can “call” on an object.
Real-life example
A cat can do stuff: it can meow, run, eat.
public class Cat
{
public string Name;
public void Meow()
{
Console.WriteLine($"{Name} says: Meow!");
}
public void Sleep()
{
Console.WriteLine($"{Name} is sleeping.");
}
}
Usage:
Cat cat1 = new Cat();
cat1.Name = "Barsik";
cat1.Meow(); // Barsik says: Meow!
cat1.Sleep(); // Barsik is sleeping.
Each object has its own methods, and they use its personal fields!
3. Static fields: shared data
Sometimes you want to store not personal data for each object, but info that’s shared for the whole class. For example, count how many cats have been created, or set a “max volume” for all cats.
For this, C# uses static fields — you declare them with the static keyword.
public class Cat
{
public string Name;
public static int Count = 0; // Static field
public Cat(string name)
{
Name = name;
Count++; // Increase total cat counter
}
}
//working with Cat class
Cat c1 = new Cat("Barsik");
Cat c2 = new Cat("Murka");
Console.WriteLine(Cat.Count); // 2 — shared for all objects
A static field is one shared for the whole class, all objects “share” it.
You usually access it via the class name: Cat.Count.
4. Static methods
A static method is a method that isn’t tied to a specific object, but belongs to the whole class.
- You can call this method without creating an object.
- Inside a static method, you can’t access regular (non-static) fields and methods — because there’s no specific object!
public class MathUtils
{
public static int Add(int a, int b)
{
return a + b;
}
}
Usage:
int sum = MathUtils.Add(2, 3); // 5
Static methods are often used for utilities — functions that don’t depend on object state.
You already know a bunch of static methods:
Console.WriteLine();
Console.ReadLine();
Math.Round();
Math.Truncate();
5. Comparing methods and fields
| What is it? | How to declare? | Where is it stored? | What for? | How to access? |
|---|---|---|---|---|
| Object field | |
In every object | Personal data of the object | cat1.Name, cat2.Name |
| Object method | |
In every object | Actions that use object fields | cat1.Meow() |
| Static field | |
One for the whole class | Shared data for all objects | Cat.C |
| Static method | |
One for the whole class | Shared “skills” that don’t depend on the object | Cat.Method() |
7. Illustration
Regular fields and methods:
. +-----------------+
cat1: | Name = "Barsik" |
| Age = 2 |
+-----------------+
+------------------+
cat2: | Name = "Murka" |
| Age = 5 |
+------------------+
Static field Count:
Cat.Count = 2 // one shared for all cats
Calling methods:
cat1.Meow(); // Barsik says: Meow!
cat2.Meow(); // Murka says: Meow!
Calling a static method:
MathUtils.Add(2, 3); // 5
Summary
- Fields — are for the individual characteristics of each object (properties).
- Methods — describe what the object can do.
- Static fields — are good for storing “shared” data (like a counter, global settings).
- Static methods — are used for operations that don’t depend on a specific object (like math functions).
8. Frequently Asked Questions
Can you access a static field through an object?
Technically — yes (cat1.Count), but it’s best to use the class (Cat.Count) so you don’t confuse yourself or others.
Can you call a non-static method without creating an object?
Nope, because that method always works with a specific object and its data.
Can you use regular fields and methods inside a static method?
No — because a static method doesn’t know which object to use.
Can you make a “fully static” class?
Yep, you write static class — you can’t create it with new, and all members of that class must be static.
GO TO FULL VERSION