Introduction & Overview
Object Orientation & C# Classes
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.
- 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
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.
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.
The GitHub repository for this course can be found here.
I'll start by creating a new folder for this course called IntroToOOClasses and then open a terminal in that folder to use the dotnet new command to create a new console app called Goto. Instead of using the code dot command as I did in previous demos to open the project in Visual Studio Code, I'll drag the project folder onto VSCode. As usual, I need to add the C# assets. Next, I'll grab the Arrays code from the data types and variables course. If you don't already have this code, you can copy it from the Cloud Academy GitHub repository https://github.com/cloudacademy/csharp-datatypes-variables. Go into the Arrays project, click on program.cs to open it, and copy the static void main method code, remembering only to include the first closing curly brace. I'll remove the code before the checker's line and then run the program to refresh our memories of what it outputs. Now, let's change the code to produce the same result by reusing just four of the checker's array element assignment statements.
I will use one of the first branching constructs developed in software programming, called a goto statement. While an oldie, it's definitely not a goodie, and you should never under any circumstances do what I'm about to show you. Honestly, I was a little surprised that C# supported this, but this how things were done many decades ago.
I'll declare an int variable y initialized to 0 for counting the checker rows. Next, a bool variable for setting the checker array element value, so where currently 1 or 0 is assigned. PopulateChecker, followed by a colon, is a label that specifies a position in the code. Y equals 0, so I can substitute the zeros in the first four checker assignment lines with y. Then I'll use Convert.ToInt32 to change the Boolean value squareValue to an int data type. When a bool is changed to an int, true becomes 1, and false becomes 0. We want the first array element to be 1, and to alternate between 1 and zero, I'll use the ! operator to negate squareValue - not true is false, and false converted to an int is 0. Ok, that's the first row done, and I can get rid of the following three checkerboard rows as I'm going to reuse the first one. The second row starts with zero, so I'll set squareValue to false, which I can do by setting squareValue to be not squareValue. Also, I need to increment y to assign values to the next row of the checkerboard.
Then we need to repeat the array assignment process, which I'll do by using the infamous goto statement, telling the code to go back to PopulateChecker and continue executing. You may have spotted a problem going back to PopulateChecker will mean the code will keep going in an infinite loop. It won't really because as soon as the code tries to access array elements beyond the defined array boundaries, an error will occur. I'm going to use an if statement to conditionally break out of the loop by going to another label called Finish. If statements are one of the critical elements of programming, enabling different code segments to be executed based on some condition. In this case, when y is greater than three, we are done, so jump to the Finish label and continue execution by writing checkers to the Console. An if statement is the keyword if followed by some Boolean expression enclosed in brackets that evaluates to either true or false. Theoretically, there is no limit to the complexity of the Boolean expression. Practically, it shouldn't be so complex that it takes an hour to figure out what is going on. When y is still less than three, we go to PopulateCheckers. Let's save and run - the same result with less code. In this demo, we've touched on conditional evaluation with the "if" statement, one of the cornerstones of programming. We've seen how we can repeat an action by branching using a goto statement, which is exceptionally bad practice, and I will never mention goto again.
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.