C# Tutorial – Part 2.2: Class

In the last post, I introduced to you some basic concepts about method in C# programming language. Also, you knew why the method is necessary and how you can use it to make an efficient program. In this second post of the #2 tutorial, I will introduce to you some basic concepts about class in C# programming language.

What is the class in C#? Why do we need it? How to use it?

class in programming is a blueprint which is used for creating an object in a particular program. So, what does the object contain? It contains the kinds of data and functionality (many methods). An object knows things and does things. For example, a calculator knows the inputs and calculates those inputs based on the given function (method).

The refactoring code in the last post

The refactoring code in the last post

In the last post, I showed you the refactoring code of the returned-value method which is used to solve the linear equation. In this case, we have 3 elements (a, b and c) and the program looks a bit short. However, if the number of elements increases to more than 3 elements, the code will be getting longer (many lines). If we, for example, have 6 elements, we will need to initialize/declare twice the group of elements – a, b, c and the array. It means that the length of the code will be extended and it looks inefficient. The efficient way to solve this problem is that we use the concept of class in C#. If we have a class to create an object that contains 3 elements and calculates the linear equation, we will only need to create an object and call the method in it every time we have 3 new elements. It will be fast and efficient when there are many objects in 1 program. That’s why we need the class. Let’s jump into the code to make more sense.

Creating a new class

Creating a new class (step 1)

To create a new class – aside from the main class, you can right-click on the project folder and choose the option as the image above.

Creating a new class

Creating a new class (step 2)

Then, you have to choose the “General” section on the left side and the “Empty Class” section in the middle. The name will be the name of the class you created. As a result, it should capitalize on each word and there is no space between words. Make sure you click on the “New” button to create it.

A new class (first view)

A new class (first view)

After creating a new class, you will see this on your screen. There are a few things you need to know in this image.

  • It has the same namespace as the main class or other upcoming classes.
  • The name of the class is the name you created (in step 2). Importantly, it will be used for creating an object in the main class.
  • A method without returned type and with the same name as the class name is called a constructor.
A new class (implementing 1)

A new class (implementing 1)

As mentioned above, a class is a blueprint and used for creating an object which knows things and does things. From the image above, there are a few things you need to know.

  • Field: This is a way you declare elements/data for a class, meaning how an object knows things. The syntax for  this is[modifier keyword][data type][element/variable's name].
    • Access modifier keyword decides an accessibility level of each element inside 1 class. There are 6 common access modifier keywords – public, private, protected, internal, protected internal, private protected. I will introduce 2 of them – public and private – in this post and go deeper in another tutorial. You can learn more at Microsoft .NET.
    • The public keyword can be used when you want the element to be accessed by other classes. Normally, I prefer to use this keyword for methods rather than for elements in a particular class.
    • The private keyword can be used when you want the element to be accessed only by the class itself. Normally, I use this keyword for either element or method. If I use the private keyword for element, I want that element to be unknown for other classes but the class containing that element. If I use the private keyword for method, I want that method to be used only inside the class containing it.
    • The element’s name should be following this format [ _name ] so that it looks more practical.
  • Constructor: This is a way you initialize/assign the value for each element inside the class. It has the same name as the class and no returned type. Importantly, the class is not required to have a constructor if it doesn’t contain any elements. The syntax for a constructor is [public] [constructor's name][(parameters)] { ... }.
    • The parameter of the constructor can be empty if you don’t want to pass any elements to your class. In the code above, I pass an integer array to the class because it needs an array that contains 3 integer elements.
    • The constructor will be the thing which is called first when an object is created. Therefore, we tend to use it to initialize/assign the value for each element.
  • Property: This is a way that helps you [access|change] the value of private elements in a particular object. It looks like a normal method, but it doesn’t have a parameter. Importantly, it contains 2 methods – get and set. The syntax for this is [public][data type][property's name] { get {...} set {...} }.
    • The get method is how you access the data. Therefore, it contains the return keyword.
    • The set method is how you change the value of the data. Therefore, it will have a syntax [element's name] = value. It looks like a void method.
    • If the property has only “get” method, we will call it the readonly (read-only) property.
A new class (Implementing 2)

A new class (Implementing 2)

Now, we will know how an object does things. By implementing some methods in the CalculateTool class, the CalculateTool object will be able to do things followed by its functionality. The code above is the one I took from the main class. It means that I already removed that from the main class. There are a few things you need to know in the image above.

  • The access modifier keyword for each method is dependent on your desire. In this case, I want an object to print the result after calculating and I don’t want to know its process. As a result, I use the public keyword for the printOutResult method and the private keyword for other methods. In the printOutResult, I will call the other private method so that an object can start the calculating process. By doing this, every time I create a new object, I only need to pass an array of 3 integer elements and call the printOutResult method. It looks more efficient than calling 3 methods in sequence.
  • The name of variables/elements in each method [method’s elements] must not be similar to that of variables/elements in the class [class’s elements]. For example, line 29 – int[] localArr = _arr. If I use _arr in the swapArray method, I directly change the value of the class’s element inside the method.
Creating an object

Creating an object

Now, you will know how to create an object after constructing a CalculateTool class. The syntax for this is [class's name][variable's name] = new [class's name][(arguments)]. There are a few things you need to know in the image above.

  • The new keyword can be used when you create a new object (I introduced in the #1 tutorial).
  • The passing values in the argument of an object are similar to the one in the parameter of the constructor inside the class.
  • [line 19 and 30] are the way to use a get method inside the ArrayProp property. The syntax for this is [object varialbe's name][.][property's name].
  • [line 29] is the way to use a set method inside the ArrayProp property. The syntax for this is [object variable's name][.][property's name] = [the desired value].
  • [line 25 and 36] are the way to call a public method inside the CalculateTool class. The syntax for this is [object variable's name][.][method's name][(parameters)]. This is a void method, which returns nothing, so we don’t need to create a variable to contain a returned value.
The output of the program

The output of the program

These are a few essential things you need to know about class in C# programming language. I encourage you to learn more on Google or Microsoft .NET and give a try by making your own program from what you have learned.

That’s it for the second part of the #2 tutorial. Hopefully, it helps you make more sense about class in C# programming language now. I will publish the #3 tutorial soon. If you have any questions or any topics you want to learn more, please let me know.

If the images in this post are unclear, I have uploaded them to my Github repository. You can visit this link to have clearer images.

Thank you for reading.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s