What is a Class? (C#)
Tags: programming, tutorial, beginners, csharp
Published: 2020-11-17 15:02:02

# 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.](https://upload.wikimedia.org/wikipedia/commons/thumb/3/34/Labrador_on_Quantock_%282175262184%29.jpg/800px-Labrador_on_Quantock_%282175262184%29.jpg)

## 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](https://dev-to-uploads.s3.amazonaws.com/i/07ctdhndvfescdppyz9b.PNG)
---------

### 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](https://dev-to-uploads.s3.amazonaws.com/i/aba16lgshxrbeliduvuc.PNG)
---------

### 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](https://dev-to-uploads.s3.amazonaws.com/i/jcwr7r7mjsy8wotk867p.PNG)
---------

### 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](https://dev-to-uploads.s3.amazonaws.com/i/gjg8h8g79zmh9n7gg17p.PNG)
---------

### 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](https://dev-to-uploads.s3.amazonaws.com/i/wd4vxlwtpnqhofoo8kpo.PNG)
---------

### 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](https://dev-to-uploads.s3.amazonaws.com/i/zros06tcorc9kt54ocgj.PNG)
---------

## 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.