How To Create a Class In C# — Beginner Friendly

Olivia
3 min readOct 14, 2021

A beginner friendly breakdown of creating classes and objects in C#

What is a class?

A class is a blueprint for a data-type (data-types such as string, integer, Boolean .etc). By creating a class, you are creating your own data-type.

View the example of a class below, lets work through it.

class Box {   public double Length { get; set; }   public double Breadth { get; set; }   public double Height { get; set; }
public double getVolume(){ return Length * Breadth * Height; } public double getArea() { return Length * Breadth;}}

Here we are creating a data-type that will represent a Box, using the class keyword

class Box

Next, we add the properties (or fields) we want our Box class to have. As best practice, properties of classes should have capital letters e.g. Height

public double Height{ get; set; }

What is get and set?

The get allows you to retrieve/get the value (such as being able to write it in the Console), set allows you to assign a value to the field, like if you wanted Length to equal 10cm.

public double Length { get; set; }

If you remove get:

public double Length { set; }

You will get an error when doing Console.WriteLine(Length) — you do not have permission to ‘get’ the property. It is read-only, but you can still set it equal to 10cm or any other value.

If you remove set:

public double Length { get; }

You only have permission to get the value. You can not assign anything to it.

How to add a method to a class.

In our example we have a method that calculates the volume of our box.

This method will return a double, we stated that after the public keyword on the first line.

We don’t need to pass Length, Breadth or Height as parameters since they were all crated in the Box class alongside with this method (view example above as reminder), and therefore the method has access to these properties.

public double getVolume(){return Length * Breadth * Height;}

Congratulations! You have created a class. Now, let’s put it to some use.

Take a look at this code which will be utilising our Box class, by creating a new instance of the class — an object.

namespace Example {class Program {
static void Main(string[] args) {
Box boxObject = new Box();
Console.WriteLine("Enter a length: "); double n1 = double.Parse(Console.ReadLine()); Console.WriteLine("Enter a breadth: "); double n2 = double.Parse(Console.ReadLine()); Console.WriteLine("Enter a Height: "); double n3 = double.Parse(Console.ReadLine());
boxObject.Length = n1; boxObject.Breadth = n2; boxObject.Height = n3; double volume = boxObject.getVolume(); double area = boxObject.getArea();Console.WriteLine($"Box dimensions are: {boxObject.Length}, {boxObject.Breadth}, {boxObject.Height}. The volume is {volume} and The area is {area}");}}}

How to use the Box class

To use the Box class, we want to initialize an object of class type Box. The code below is initializing an object called boxObject, not a variable as such

Box boxObject = new Box();

User input and type-casting

This one is simple. We’re asking the user to entre values for each of the Box properties we created. The answer will be in the format of a string, so before we use it, we must parse it into an integer/double

Console.WriteLine("Enter a length: ");double n1 = double.Parse(Console.ReadLine());

Thanks to the { set; } we used earlier, we can assign a value to Length. We access length by using dot notation (literally a full stop) on our boxObject object and assign it the user input we turned into a double.

boxObject.Length = n1;

Similar is done for assigning a value to volume and area. Those are calculated from the values chosen

double volume = boxObject.getVolume();

Now we can view everything and write it to Console! Here’s an example out if you gave Length 5, Breadth 10, and Height 10:

Enter a length:
5
Enter a breadth:
10
Enter a Height:
15
Box dimensions are: 5, 10, 15
the volume is 750
the area is 50

--

--

Olivia

From Law Graduate to Fullstack Developer / Professional Googler. Join me as I blog about the highs & lows of learning to code and navigating my new career path