From the course: Learning C#

Defining C# classes - C# Tutorial

From the course: Learning C#

Defining C# classes

- As I mentioned at the start of the course C-sharp is an object-oriented language. All of your code lives in classes and all the data types are fundamentally objects, and so on. In this chapter, we're going to learn how to work with C-sharp classes which are the blueprints for creating your own objects. And again, I just want to remind you that before you start this chapter you should already be familiar with the principles and ideas of object-oriented programming. If you're not, then check out the programming fundamentals object-oriented design course. So let's start by learning how to define a class in C sharp. So here in our editor, in the classes folder under the defining folder, let's open up our program file and this file named book dot CS. So our sample class will contain information about a book. To define a class in C-sharp, you use the class keyword followed by the name of the class, which is book in this case. And we're going to make our class public so that any other code in our program can use it. And we'll learn more about the concept of these access modifier keywords a bit later in the chapter. So inside our class definition we can declare member variables, sometimes called fields, that will hold data related to the class. So for our book, we'll declare fields to hold the name which will be a string, and I'm going to use an underscore to indicate that it's an internal variable name. And then I'll have another string for the author, and an integer for the page count of the book. Classes also have a function called a constructor. This is the function that is used to create an object of this classes type. So once again, we'll have that be a public function and it's the name of the object and it can take parameters. So we'll pass in the name, author, and page count of the book. And then inside the constructor, we'll simply initialize our internal fields to each of these parameters. So, we'll have author, and then finally page count is going to be equal to pages. And then finally we can declare other methods that operate on the class. So let's add a method called get description that will return the book, name, and author and we'll use the string interpolation feature that we learned about earlier to do that. So this is going to be a public function. It's going to return a string and we'll call it, get description. And it's going to return an interpolated string and that's going to be underscore name by underscore author. So that completes the class definition. So let's go over to the program code and try out our class. So here in the main code, I'm going to create some instances of our book object and to do that I'm going to use the new keyword. So I'll declare a variable of type book. I'll call it b1, and then I'll make a new book and then I'll give it a name, so it'll be, let's see "War and Peace," and that was written by Leo Tolstoy. And that's pretty big, it's like 800 pages. And then I'll copy that and we'll make another book, call this one b2, and this will be "The Grapes of Wrath," and that of course was written by John Steinbeck, and that's I think 464 pages. Now let's call the get description method on each object. So let's print that out. We'll do b1 dot get description and then we'll do the same thing for b2. So let's go ahead and run what we have. So I'll open this up in my integrated terminal and we'll dot net run this. All right, and when the code runs, you can see that we are able to successfully define our book objects and get their descriptions. So you can see here in the output that that string is being printed out, so everything seems to be working. So let's go back to the code and try something. I'm going to try and change one of these member fields directly, right? So I'm going to try and change the name. So if I type something like b1 dot underscore name equals and then I'll put in some other author, like Aldous Huxley. Now this isn't going to work, okay? It's going to cause an error, and you can see that when I try to run this, we get an error and it says, book underscore name is inaccessible due to its protection level. So something is keeping my code from accessing this variable, this field name in my class. And we're going to learn more about that in a separate video.

Contents