Join Alexander Zanfir for an in-depth discussion in this video Introduction to object-oriented programming, part of Learning C#.
- [Instructor] The school client was thrilled with the upgrades you made to the student grading program. Now just like we did before, let's look at the code in detail to see if we can re-factor anything at all. The first hint usually is that there's some duplicate code, so let's see if we can notice any. It looks like we're creating two lists, one for student name and another for student grade, but they are both to do with the student. Wouldn't it be great if we could just make one list? But how can we do that when we have two pieces of information to store? Just as you're thinking about that improvement, the school client calls and asks for some new features.
They want to store the student's birthday, address, phone number, and they mention that they will probably be adding more information to each student soon. Creating a new list for each of these information types is really going to bloat your code. So in order to create a single list instead of five lists for these five properties of the student the teacher wants to store, we will need to create a single list that holds a variable or data type that can store all five types. But up until now we have only used data types that have a single property, such as an integer or string.
We could use an array, but that's not very intuitive. So maybe we can create a custom data type that stores the name, grade, birthday, address and phone number. Using object oriented programming, we will create our own object type that will have all of those properties. But first we will need to create a definition of those different properties we want to include in it. This is called a class. You might have noticed the keyword class already in all of our programs, such as here on line six.
That means our main function that we've been using this entire time is actually part of a class already called programming. The file that we have been using is called program.cs as we can see here. The file type .cs is actually for class. So all of our code in C# is actually written inside classes. We've been working in a class this entire time, and will understand the consequences and need for that later in this course. But for now let's continue to make our own class below, and we'll call it student.
So let's scroll down below our existing class, and we'll create a new class. Now let's add all of those properties or attributes we spoke about. Just as with our arrays and lists, name will be of type string, and grade will be of type int. And we'll set birthday and address as type string, and phone number, since it's all numbers, to integer. We will be using the public keyword to make these properties usable by another class, since we will be trying to use it in our program class we looked at above.
But we'll look at the public keyword in more detail later in this course. Now let's create or instantiate an object from our new class to test it out. So let's scroll to the top of our main function. At the top I'll create a student variable, and I'll set it to new student. And as a test, we'll set the value for student name.
As you can see, to access the name property, we'll use the dot operator after specifying the object variable name. And then we'll just specify the property dot name. And now as a quick test, let's console write the student name. Let's see if that works. And before we ask for the student name with our previous code we can see that our test name is shown. As you can see our class allowed us to make a new object that guaranteed it had the properties we specified in the class.
It's sort of like a blueprint. Now that our test worked, let's get rid of it. And instead we'll make a student list. This student list will replace both of our student names and student grades list we previously had. So for now we'll write it above. We'll call it students, we'll make a new list, and we'll make it type student. And then let's get rid of the previous lists. Then at the top of our while loop, let's create a new student.
And I'll call this variable new student, and set it to a new object we create of the class student. Then on line 19, instead of adding to the students list, I'll just set the name from our new student object using the dot operator. And then we'll repeat that for student grade.
So now we have this new student, and it has some of its properties filled out already. Before we continue to ask for the other properties, let's try to get our program working so we can possibly re-factor first. So next let's add our new student to the students list. And then we'll continue on and ask if they'd like to add another. Now let's go down to our for loop where we display the student name and grade. And we could continue to use the for loop, but since we have one list now, it's better to use a for each.
So I'll get rid of line 32 and I'll type in for each, and hit tab tab. And rename item to student in our students collection. Now let's access the student name through student.name, and the grade as well. As you can see already our code is a lot easier to read. It's a lot more descriptive, and new programmers won't have to spend too much time analyzing what the code actually does.
So let's go ahead and try out the program as it is now with just the two properties. And we can see it's behaving the same. Let's go ahead and add the other properties. So I'll copy line 21 and 22 right below, and then let's ask for birthday. And it'll be a straight string, so we don't need int parse, and I'll change the property to dot birthday.
And let's paste another for address. And again, it'll be a string, so we don't need int parse. And one last one for phone number. And in this case we are required to provide an int, so we will use int.parse. Now let's give this a try. We can see that it's behaving as expected, and we were asked all of the properties from our class.
Once we've entered them, we did get our recap, with just the name and the grade. So from two lists we are down to one. And the code is more intuitive since you can clearly see that the name property is now part of a new student we are about to add to the student list. Let's go take a look at our student class. You might have noticed that we specified the data type for the variable we created in our class when making our property. Yet in the rest of our code, in our non class code, we always just used the var keyword to let C# pick the right type for us.
That's one of the differences between a class variable and a local variable we have outside of the class. We can't use the var keyword for our class variables. If you think about it, it makes sense. We are defining a blueprint with the class for future objects we will want to make or instantiate. And blueprints need to have every detail defined. We have to specify exactly what type each property will be so we can guarantee that objects of that class match the blueprint exactly.
This is part of a bigger concept that C# uses which is known as type safety, and this enforces the type of data we use in specific variable types. Next let's look at another great feature offered by object oriented programming.
Explore variables and data types; controlling program flow with conditions and loops; and building functions. Learn how to implement object-oriented programming such as encapsulation and inheritance in C#, and find out how to debug your code. Alexander then explores advanced concepts such as enumerators, ref parameters, interfaces, events, and abstract classes. In the last two chapters, he covers the "top" features introduced in the last several versions of C#, including lambda expressions and string interpolation.
- C# variables and data types
- Switch statements
- Object-oriented programming: encapsulation, properties, and inheritance
- Debugging C# code
- Advanced C# concepts
- Top new features