Writing the Class in C#
Turn a class design into C# with private fields and public methods, then create objects with new and call their methods.
Apprentice · C# · OOP · Updated
After this lesson you can
- Write a class with private fields and public methods
- Create an object with new and call its methods
Key takeaways
- Declare a class with
classand a PascalCase name, and put its fields and methods between its curly braces. - Mark fields
privateso only the class's own code can use them. That's encapsulation, and outside code that tries doesn't compile. - Methods that use an object's fields aren't
static: each one works on the object it's called on, such askitchenFridge.AddItem(400). new Refrigerator()creates an object. Each object has its own fields, so changing one doesn't touch another.
Now turn the Refrigerator design into C#.
Declaring the class
A class is declared with the word class and its name, in PascalCase. Everything the class knows and does goes between its curly braces:
public class Refrigerator
{
// the class's fields and methods go here
}
In a project, each class usually lives in its own file, named after it: Refrigerator.cs. In a single-file program, write your top-level code first and your classes at the bottom of the file.
Fields: what it knows
A variable that belongs to an object is a field. Declare fields inside the class, outside any method. Mark them private, so only the object's own methods can use them:
public class Refrigerator
{
private int temperature = 37;
private int capacity = 1500;
private int usedSpace = 0;
}
public and private are access modifiers. They decide who may use a member:
| Modifier | Who can use it |
|---|---|
public |
any code, anywhere |
private |
only code inside this class |
Keeping fields private is called encapsulation: the object's data is sealed inside it, and the outside world works with it only through the methods the class chooses to offer. Nobody can set usedSpace to -400 by accident.
Methods: what it does
Methods inside a class can read and change its fields. They aren't static: each method works on one particular object, the one it was called on.
public class Refrigerator
{
private int temperature = 37;
private int capacity = 1500;
private int usedSpace = 0;
// Adds an item if there's room: returns true if it fit, false if not.
public bool AddItem(int itemSize)
{
if (usedSpace + itemSize <= capacity)
{
usedSpace += itemSize;
return true;
}
return false;
}
public int GetFreeSpace()
{
return capacity - usedSpace;
}
public int GetTemperature()
{
return temperature;
}
}
Creating an object
A class is a new type, so you can declare a variable of that type. To create an object, an instance of the class, use new and the class's name followed by parentheses:
Refrigerator kitchenFridge = new Refrigerator();
bool fitted = kitchenFridge.AddItem(400);
Console.WriteLine( quot;Milk fitted: {fitted}");
Console.WriteLine( quot;Free space: {kitchenFridge.GetFreeSpace()}");
Console.WriteLine( quot;Temperature: {kitchenFridge.GetTemperature()}");
public class Refrigerator
{
private int temperature = 37;
private int capacity = 1500;
private int usedSpace = 0;
public bool AddItem(int itemSize)
{
if (usedSpace + itemSize <= capacity)
{
usedSpace += itemSize;
return true;
}
return false;
}
public int GetFreeSpace()
{
return capacity - usedSpace;
}
public int GetTemperature()
{
return temperature;
}
}
Milk fitted: True
Free space: 1100
Temperature: 37
You call an object's method with a period: kitchenFridge.AddItem(400), just as you call name.ToUpper() on a string.
Each object has its own state
Two objects made from the same class are separate. Changing one doesn't touch the other:
Refrigerator kitchenFridge = new Refrigerator();
Refrigerator garageFridge = new Refrigerator();
kitchenFridge.AddItem(1000);
Console.WriteLine( quot;Kitchen: {kitchenFridge.GetFreeSpace()} free");
Console.WriteLine( quot;Garage: {garageFridge.GetFreeSpace()} free");
public class Refrigerator
{
private int capacity = 1500;
private int usedSpace = 0;
public bool AddItem(int itemSize)
{
if (usedSpace + itemSize <= capacity)
{
usedSpace += itemSize;
return true;
}
return false;
}
public int GetFreeSpace()
{
return capacity - usedSpace;
}
}
Kitchen: 500 free
Garage: 1500 free
Private really means private
Code outside the class can't touch a private field. This doesn't compile:
Refrigerator kitchenFridge = new Refrigerator();
kitchenFridge.usedSpace = -400;
public class Refrigerator
{
private int usedSpace = 0;
}
The compiler stops it with 'Refrigerator.usedSpace' is inaccessible due to its protection level. That's encapsulation doing its job.
Check yourself
2 questions to try on the lesson page.
So what
A class bundles state and behavior into one type: private fields hold what it knows, public methods offer what it does, and new makes as many independent objects from it as you need.