From the course: Advanced C#: Object-Oriented Programming
Defining classes - C# Tutorial
From the course: Advanced C#: Object-Oriented Programming
Defining classes
- [Instructor] In this chapter, we're going to review some of the basics of object-oriented programming as they apply to C#. If you're already familiar with the basic concepts of object-oriented programming in C# and .NET, then you can feel free to skip over or skim this chapter or come back to it as you need to if you want a refresher. We're going to start by reviewing how to create classes and objects in C#. So here in my editor I'm going to open up in the Start folder in chapter one, I'm going to open up the code in my Defining Classes folder. So let's go ahead and open up my shapes.cs file. So to define a class, we're going to use the class keyword. So I'm going to create a class that will represent a rectangle. All right, and I'll put this closing bracket down here. All right, so to define a class, we use the class keyword. And typically when you define a class in C#, you will also create what is called a constructor function for that class. It uses the same name as the class itself and can accept zero or more parameters and is usually used to initialize the state of the object when it is created. So I will define a constructor for this class that takes two arguments or parameters for the width and the height. So this is going to be my rectangle and it's going to take integer for the width and an integer for the height. And I'll also add two integer member variables to my class to hold that data and I'll put those down here. So I'll have a public int width, and I'll also have one for height. And we're going to learn more about member variables and properties later in the chapter, but for now, you just need to understand that a class can define variables that go along with each instance of the object that gets created. So in the constructor, I will initialize those two members. So width will be equal to W, and height will be equal to H. The constructor, by the way, is not required. If you don't define one, then the C# compiler will create a default constructor for you. And I can also define more than one version of the constructor. So for example, a square is just a special case of a rectangle where all sides are of equal length. So I can define another constructor and I'll call this one once again Rectangle. And this version will only take one argument called side. And I'll just set both values, width and height, equal to the side parameter. And you've probably noticed by now that I'm using this keyword public when I define my functions and my variables. This is something we'll also examine more later on in the chapter. But for now, all you need to know is that when I declare these functions, I have to make them public so that I can access them from outside the code that is defined within the class itself, but we'll cover that a little bit more later. And then finally, let's add something useful to the class to make it do something. I'm going to add a function called GetArea to return the area of the rectangle. And I'll make that a public function that returns an int and it's going to be called GetArea and it's simply going to return the width times the height. All right, so now we have a class that we can use to create objects and it actually does something useful. So let's go over to the program code. So I'm going to save this and then open up my program.cs file. And let's write some code to create a couple of rectangles. So I'll write the class type and rect1 and I'm going to use the new operator to create a new rectangle, and that one's going to have a width and a height of 10 and 20. And then I'll make another one called rect2 and I'll use the second constructor to make a square with size equal to 30. And then let's also exercise the GetArea function. So we'll write out to the console, rect1.GetArea, and we'll make a copy of that. And we'll do the same thing with rect2. All right, so now we're in a position where we can exercise the code. What I'm going to do is right-click on DefineClasses and choose Open In Integrated Terminal. And that will bring up my built-in terminal here in Visual Studio Code. And then I can just type dotnet run and that will run my examples. And you can see in the output that we have the area of each rectangle. So the first one is area 200 and the second one is area of 900. All right, so let's go ahead and just change the values of the width and height. So for rect2, I'll change the width to five and rect2's height is going to be seven. And then let's just go ahead and write the area out again. And when I run again, now you can see that the area of rect2 has changed. It started out as 900, now it's down to 35, right? So let's quickly try something else. Let's see why that public keyword is important. What I'm going to do is go back to my shapes code and I'm going to scroll down and I'm going to remove public from these two variable declarations and then I'll try to run my code again, all right? And when I try to do this, so if I do dotnet run again, you'll see that I'm getting an error because if you don't explicitly declare the access type of a function or a member variable, they default to being private to the class and are not accessible outside the code that is within the class definition. So by making, let's go back to my shapes code, so by making these private, right? I could also just simply declare them as private which we'll learn more about later, but if I don't put any descriptor on here which indicates their access level, they default to being private. So to make sure that I can access them from outside the class code, I have to put public here and when I do that, all right, so let's try one more time. And now you can see that the error is fixed. And again, we're going to learn more about that later in the chapter. But this short sample summarizes how to define and instantiate a class.