loading...
Cover image for What is a Class? (C#)

What is a Class? (C#)

edvin profile image Edvin ・4 min read

Introduction

Object-Oriented Programming (OOP for short) is something many of us are introduced to early in our programming journey.
A particular term that is very common to hear about, are classes.

Definition

A class is a blueprint of an object that we can imagine.

There are countless definitions of what a class is.
If you are having trouble grasping the concreteness of it, do not worry.
Sometimes it is best to learn by looking at examples, and not spending five and a half hours on a Tuesday night researching - just so you can finish that stupid school essay.
That is based on a true story.


In programming, the terms class and object are often used together. This is because:

  • A class is a blueprint for an object. (The "concept" of a dog)
  • An object is a particular instance of a class. (Your neighbors white labrador Willy, that barks consistently at 3AM)

That loud dog I mentioned.

Examples

Dogs, sailboats, cake and machine-guns are all examples of what a class could be.
There are an endless amount of options, as it is up to you as the programmer to define them in your application.


Let us use a book as an example. A book is a real-world object that is commonly found all across the world.
In order to make a class that represents a book, we need to think about what properties a book has. When I say 'properties', I really mean the features that makes a book - a book.

For myself, I can think of these:

  • Author
  • Date published
  • Cover photo
  • Number of pages

Let us now try to implement the concept of a Book into code.

Code 1 - Getting into the Code

I have created a project called "Playground" in Visual Studio 2019. After creating this project, I went to File -> New -> File and then selected the "Class" option.
I named mine "Book", and it gets the extension .cs. This is common for C# classes.

The values arranged in the Book.cs class

Code 2 - Constructor

A class needs a constructor in order to define how an object is created. This is where you pass in the specific characteristics for a particular object.
The constructor is seen on line 14 to line 20, and only has the job of setting the properties we give it, to that book-objects values.

A constructor for our book class

Code 3 - A method

Let us now create a method that will print out a book's values to the console window. A method is a way for an object to perform a set of actions / instructions.
The PrintBookDetails() method only has the job of printing these values:

  • Author
  • Published date
  • Number of pages

As an "Image" would be strange to print to the console window, I have ignored that value for now.
NOTE: The Image class is a class I created myself. If you try to replicate these steps, you might see that your code will get red underlines here.
If so, just ignore the cover-image property of the Book class.

A method that performs a set of instructions, in this case printing values to the screen

Code 4 - The entry-point

This application has a class called "Program.cs". This is a file you get after creating a new Console Application in Visual Studio.
private static void Main(string[] args) might look weird, but all you need to know is that it is the application's entry-point.
Everything inside the
private static void Main(string[] args)
block defines what happens the moment your program is being started, like when you double-click a program on your desktop.

In our case, we are just creating a Book object in the way we defined it in our constructor earlier.
This book has the values:

  • Albert (for the Author-name)
  • The date 17th of November, 2000 (for the published date)
  • A cover-image :)
  • 146 pages

The entry-point, i.e what happens when we run our program

Code 5 - Printing the values

Nothing will really happen right now as we start the program, however.
While a book-object is created from the Book class, we have not specified anything that should be printed to the screen.

Let us use the PrintBookDetails() method that we created earlier, which actually prints out values.

Using a method

Code 6 - The result

By now running our program, we can see that the console window displays the properties of Albert's book.

Values being printed onto the screen

Wrapping up

This is just one example of how you could model a class in your application. I highly encourage you to do a class-modelling exercise yourself.
Look around in your room, and fixate on an object you see. Write down its properties, and consider how you could implement this in code.

I am new to writing articles like these, so I would love your feedback if you have any. :)

Feel free to contact me if you have a question about this article (or what I discussed), and I hope you have a great journey with your dream-project.

Discussion

pic
Editor guide