Skip to content

Introduction to classes and objects in C#

Become a developer with our complete learning paths
Become a developer with our complete learning paths

Intro

Object-oriented programming (OOP) is a programming paradigm that focuses on the concept of objects, which are instances of classes that contain data and behavior. C# is an object-oriented programming language that provides robust support for OOP concepts. In this article, we will explore some of the fundamental concepts of OOP in C#, including objects, classes, and instances.

What is a Class

A class is a recipe or a template that defines the properties and behavior of objects. It provides a structure for objects to store and manipulate data. In C#, a class can contain fields, properties, methods, events, and nested types. A class can also inherit properties and behavior from a base class, known as inheritance.

For example, let’s imagine we want to write a class Cake. It is not a particular cake, just an abstract one (it can be a chocolate cake, cupcake, or any other cake you want).

Let’s think about what our cake would have. I would say that a cake can have the following attributes:

  • Flavor;
  • A certain amount of layers;
  • Homemade or not;

You are already familiar with the variable and data types concepts. So before going any further, take a minute to think about what data types can be used to represent the attributes mentioned above.

I hope you have tried. And this is my turn:

  • Flavor: A string that identifies the flavor of the cake.
  • Layers: An integer that identifies the number of layers in the cake.
  • Homemade: A boolean that is set to true when it is homemade. Otherwise, it is set to false.

Moreover, You already know what a method is. So now try to imagine what we can do with this cake object.

  • Cut: A method that cuts the cake into slices.
  • Decorate: A method that adds toppings to the cake.
  • Bake: A method that bakes the cake according to a specific recipe.
  • Frost: A method that applies frosting or cream to the cake.

Cool, now we can combine it and create an actual class:

What is an Object/Instance

In C#, an object is a runtime instance of a class that represents a real-world entity or a software abstraction. It contains data in the form of variables or properties and behavior in the form of methods or functions. Objects can interact with each other to achieve a specific goal.

So if the class above is a recipe, how can we get the cake? We have to use a constructor method and the “new” keyword. It will return an instance of this class. This is how it looks like:

But what is a constructor?

It is a method of a class that we call once to create an object/instance of a given class. Here we place the logic that should be done during the construction. For example, we can get and set parameters from outside the class to create an object with these specific parameters.

( link to the Constructors article )

How to use an instance of a class

Now when we have our delicious cake, let’s try to use it:

In the last line above, you can see how we get access to the attributes of a class. But the output will be null.

And there are two reasons for that:

  1. We didn’t assign our attribute. So the program doesn’t know what should be there.
  2. We used a “?” in the attribute definition that allowed us to make it null by default. If you remove it, it won’t work anymore. To make it work in that case, you need a default value.

You can give it using the following syntax:

The same we can do for the other fields as well.

And now, our program should print the default value of this field.

 

 

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide