The course is part of this learning path
This course takes code from the Data Types and Variables course and refines it using object-oriented (OO) principles. We explore some of the main concepts of OO programming during this process, such as encapsulation, code reuse, and inheritance.
Along the way, we learn more about essential code structures such as conditional evaluation with if-then-else statements, functions for grouping code that performs a specific task, and for-loops for dynamically repeating an action. We will also look at how .NET and C# have object-oriented baked-in as a fundamental design principle underpinning the framework and language.
Learning Objectives
- Understand the benefits of object orientation and what came before it
- Learn about essential code structures and turn code into a class
- Refine the class code and learn more object-oriented concepts
- Learn about inheritance, a fundamental object-oriented concept
- Understand how object-orientation is a foundation principle of C# and .NET
Intended Audience
This course is intended for those who already have an understanding of data types and variables in C# and now want to learn about object-oriented principles.
Prerequisites
To get the most out of this course, you should have an understanding of C# as well as basic data types: strings, numbers, and Booleans. In order to follow along with the demos, you should also have a working development environment, whether in Windows, Linux, or macOS.
Resources
The GitHub repository for this course can be found here.
Now for the main event. Let's turn the checkerboard code into a board class. I'll create a new console app called checkerboard and open it by dragging the folder onto visual Studio Code.
A class is a definition of an object, and an object is both a variable and functionality, although, in the context of classes, functions are called methods. It will come as no surprise that a class is defined with the keyword class followed by the class's name. Everything inside the curly braces is the class definition. Within the class, we define variables to hold an object's data. A class can be likened to a compound variable made up of other variables as a molecule is made up of atoms. Having said that, variables inside a class don't have to be simple data types like numbers or text. A class definition can contain other classes as variables. Strictly speaking, variables defined in a class are called members. Member variables are also defined by their visibility. Checkers, rows, and columns are private, which means they are only visible and used within the class. Members can also be defined as public or protected, which we'll see later. Every class has a constructor function that runs when a new instance of the class is created. The constructor has public visibility, so code external to the class can make an instance of the class, and the constructor has the same name as the class. A class can have multiple constructor functions, all with the same name, as long as they have different parameter signatures. A function's signature is the number and data types of the parameters. I could have additional Board constructors with no parameters, one with an int and a string parameter, or one with three int parameters. I can't have another constructor with two int parameters, even if the parameters have different names. Because a board has to have at least some rows and columns, I've got one constructor that takes two int parameters. These parameters are only visible to the constructor function, so I'll assign them to the rows and columns members to make them visible to the whole class.
As with the previous code, I still need to create an instance of the checkers array within the board class. However, this time I'll use rows and columns to define the array boundaries, making the board size dynamic. Next, I'll create a private function Setup to populate the checkers array, using similar logic as already seen, and call Setup from the constructor. Finally, we need a print function, so I'll copy Setup with its nested for loops and enter the Console.Write statements. To use the class or put more correctly instantiate an instance of the class, declare a Board variable, and use the new operator with the class constructor. The C# language extension recognizes our class as a new data type, color coding appropriately, and the IntelliSense tooltips also recognize the board as a class. Except I can't see the print function. Why is that? Well, like private variables, private functions are only visible within the class, so I need to change print from private to public. There we go; the print method is now visible. Let's save and run. It looks like we've got a winner, refactoring the checkerboard code into a class that produces the same checkerboard pattern. We're not done yet. Next, we'll refine our code, introducing more flexibility and validation.
Hallam is a software architect with over 20 years experience across a wide range of industries. He began his software career as a Delphi/Interbase disciple but changed his allegiance to Microsoft with its deep and broad ecosystem. While Hallam has designed and crafted custom software utilizing web, mobile and desktop technologies, good quality reliable data is the key to a successful solution. The challenge of quickly turning data into useful information for digestion by humans and machines has led Hallam to specialize in database design and process automation. Showing customers how leverage new technology to change and improve their business processes is one of the key drivers keeping Hallam coming back to the keyboard.