Locked lesson.
About this lesson
We learn how to create our own class and define the attributes within.
Exercise files
Download this lesson’s related exercise files.
44 - Intro To Classes Part 2.docx60.9 KB 44 - Intro To Classes Part 2 SOLUTION.docx
58.9 KB
Quick reference
Intro To Classes (Part 2)
Classes can be created in the Solution Explorer on the right side of the Visual Studio screen.
When to use
Do this every time you want to create a class.
Instructions
{
// Create Book Object
Book book1 = new Book();
book1.title = "C# Programming";
book1.author = "John Elder";
book1.pages = 309;
Console.WriteLine(book1.title);
}
// Create Class
class Book
{
public string title;
public string author;
public int pages;
}
Hints & tips
- Class names start with Capital Letters
- Classes can be created from the Solution Explorer in Visual Studio
- 00:04 Okay, in this video, we're going to jump right in and build our first class.
- 00:07 So to create a class in C#, it's actually very simple.
- 00:10 We just come over to this side panel right here on the right hand side, and
- 00:13 right click on our project.
- 00:15 And if we look through here we can add a class.
- 00:19 And when we do, this thing pops up floating a template.
- 00:23 It's saying, hey, what do we want to add?
- 00:24 We just want to add up here at the very top, it says class.
- 00:26 We can click on this and you can come down here and
- 00:29 name this thing anything you want.
- 00:32 So what do we want to do?
- 00:33 Let's create a book class, books are pretty simple.
- 00:36 They're not that hard to model.
- 00:37 So we'll create a book class.
- 00:38 So let's add this thing.
- 00:40 And you can see right over here, boom, it pops up on the side panel.
- 00:43 It also popped up right here on the screen.
- 00:45 Now at the very top of our program right here, we see Book.cs.
- 00:50 And we also have our old Program.cs.
- 00:52 So we can toggle back and forth between these two.
- 00:55 And now whenever we create something in here,
- 00:57 we can use it in here in our program.
- 00:59 So notice right off the bat as the same namespace HelloWorld, all right?
- 01:05 It's important some things that we don't really care about,
- 01:09 the important thing right here is this class Book.
- 01:12 And classes are always started with a capital letter, so
- 01:15 if you were to do this manually, be sure to make this capitalized.
- 01:19 So inside here we create our blueprint, right?
- 01:22 What does it mean to be a book?
- 01:24 So think about a book and we're going to keep this very simple.
- 01:26 You could probably create tons of different attributes and things for
- 01:29 this book, but we're going to keep it really simple.
- 01:31 And to define these things, we define them just like we have for
- 01:36 other types of data types, but instead we start with public.
- 01:40 And you will change this in certain circumstances, but
- 01:43 this allows these things to be used by anyone anywhere, right?
- 01:46 So you may or may not want to do that, but we'll get into all that later.
- 01:49 So right now we want to create a public.
- 01:53 And this is going to be a string.
- 01:54 We want to describe the things in our book and
- 01:56 the first thing we want to describe is the book's title, right?
- 01:59 So, all right, all of our books will have a title, we want a public string.
- 02:04 And what else?
- 02:05 All of our books are going to have an author, right?
- 02:08 And let's go public, let's go int, integer and the number of pages, right?
- 02:14 So very simple, three things.
- 02:16 Our books have just three things, a title, an author, and page numbers, right?
- 02:21 So, okay, that's cool, if we go ahead and save this and
- 02:25 head back over to our regular program, we now want to create a book object.
- 02:30 We have the class, it's the blueprint.
- 02:32 It defines what it means to be a book.
- 02:34 In our case, it's very simple, books have a title, books have authors and
- 02:37 books have pages, right?
- 02:38 So now we want to create an object, a book object.
- 02:41 And you can have as many objects as you want, as many books as you want, right?
- 02:45 So we'll create one real quick here.
- 02:47 And to do this, we just call our Book class, and then name your book.
- 02:51 We're creating a book, let's name it.
- 02:53 I'm just going to call it book1, right?
- 02:55 And then we set that equal to a new Book object and that's all there is to it.
- 03:01 So now we've created a book, now this book doesn't have any information about it.
- 03:05 We don't know what the title is, we don't know who the author is,
- 03:08 we don't know how many pages it is, so we need to define that stuff right here.
- 03:11 So here we can go book1, and we can say .title.
- 03:15 And let's say C# Programming For Everyone, right?
- 03:21 That's the title of our book.
- 03:23 We could go book1.author.
- 03:26 Who wrote this wonderful book?
- 03:27 John Elder, I didn't actually write that book, but let's pretend that I did.
- 03:32 And let's go book1.pages.
- 03:36 And you'll notice as I type book1., look at our Intelisis thing here.
- 03:42 It knows it has an author, it has pages, and it has a title.
- 03:45 So we can click here for pages and it pops right up.
- 03:49 Why does our system know that?
- 03:50 Because we have a blueprint, we have created our own data types, right?
- 03:55 And the system looks for them now whenever we type in something for
- 03:59 our object here, so let's say this has 305 pages, all right?
- 04:04 So we've now defined our book and
- 04:07 we can Console.WriteLine out anything we want.
- 04:12 So let's say we want book1.title, right?
- 04:16 We can console write that out.
- 04:17 So if we save this and run it, boom, just like that, C# Programming For Everyone.
- 04:22 Very cool, and we have this, we can do the same thing.
- 04:26 We can create as many of these as we want.
- 04:28 So we can come down here and create another one.
- 04:30 Well, let's call this one book2, right?
- 04:32 So again book2, book2, book2, and
- 04:36 let's say this one is called Python Programming For Fun, I don't know.
- 04:44 And this is written by Jeff Elder, and this one only has 145 pages, right?
- 04:50 We can come down here and now we can access book2.
- 04:53 So we can call book2.title, if we save this and run it.
- 04:59 Python Programming For Fun.
- 05:01 If we want book2.author, we can just call author, save this and run it
- 05:09 Our author is Jeff elder, so just that easy.
- 05:12 We have created a class right here, the class is the blueprint.
- 05:17 It very simply just defines what our thing is.
- 05:21 In this case, it's a book, it has a title, author, pages.
- 05:23 We have three things here, you could have 100 things here,
- 05:26 you can have one thing here.
- 05:27 Doesn't matter at all, you define exactly what it means to be your book, or
- 05:31 your hat, or your car, or anything you want to model in your program,
- 05:35 you just define it right there.
- 05:37 Now in our program, we can create objects of our class.
- 05:41 And an object is just an instance of the class, it's a book.
- 05:44 We can create one book, we can create 200 books, and we do it just like this.
- 05:49 Now, this is a lot of code to create one book 1, 2, 3, 4 lines of code.
- 05:54 We can break this down into actually just one line of code using something
- 05:58 called constructors.
- 05:59 And that's what we going to look at in the next video.
Lesson notes are only available for subscribers.