Join Bruce Van Horn for an in-depth discussion in this video Creating a class, part of C# Essential Training: 1 Syntax and Object Oriented Programming.
- [Instructor] So far we've been working with puzzle pieces. It's time to put the puzzle together and actually create something in Visual Studio and C#. We're done with the interactive window. I'm going to scroll it back down here, and I'll actually go ahead and close it for now. Next let's create an actual Visual Studio project that's going to hold the classes that we're going to build as we learn to make classes in C#. I'm going to select File, New, Project. And this time rather than Console App, I'm going to go with Class Library.
We're going to create a class library, which you can reuse in lots of different kinds of software. You could use your logic that's in this library in a console app, in a desktop app, in a web app, or all three. And that really is what we're really trying to get to in the world of programming: reusable code. The next step is to name my class library. I'm going to call this SchoolLibrary cause that has a nice ring to it, doesn't it? School Library. I don't necessarily want my solution name to be the same as my library name.
Usually what winds up happening here is you make several different projects and put them into one solution, and we're looking at the library component of it. So why don't we name our solution SchoolApp. I'm going to leave these options as they are, and I'll go ahead and click Okay so Visual Studio can create the project for me. Once Visual Studio is finished creating your project, you need to find the Solution Explorer.
Mine is right here. Yours is probably already restored. I remember taking this down to size earlier when we were working with some long lines, and I just needed to bring it back. So I brought it out, I pinned it, and now it's right where it's supposed to be. So the Solution Explorer will show you everything that's actually in your solution. So you have the solution at the top, you have your project. There can be multiple projects in the solution, all of different types. Maybe some are console apps, some are desktop apps, some are web apps.
You can have all of your projects in one place under one solution. That helps you keep things organized. Visual Studio has already created a class for us. We can preview this as we like. There's nothing really to it. And so that I can teach you how to make a class from scratch, I'm actually just going to get rid of this one. I'm going to right click on Class1 and select Delete. And we'll say Yes. Now let's create our own class from scratch.
I'm going to right click on the project, and then I'm going to come down, I'm going to find Add, and then at the bottom of that is Class. The dialog box will come up. Class is the default selection, and the default name is Class1 and dot CS. Not a very imaginative name. Why don't we change this? I'm going to call this class, School.cs. Now I could leave the dot CS off and it would put it on for me. But I'll go ahead and put it there, and effectively I got the same thing I was looking at a moment ago except it's not called Class1.
From here I can add my member variables, fields, properties. All of that can be added right here inside the curly brace, which defines the class itself. We're going to do a lot more with that over the next few videos, so let's just put some things in here so we can get our feet wet. We'll put a field in here, which is just about the simplest instance variable that you can put in. I'm going to call it a string, and it's going to be called SchoolName. I can give this a few more fields as well.
Maybe the school address, which we would probably need to flesh out in greater detail later. Let's do the city, so I'll call it SchoolCity. SchoolState. SchoolZIP. Let's add a field for the phone number, and you get the idea. We can keep adding fields as we need them to.
Now this is a fairly simple class. There's not a lot going on in here yet. As I said, we're going to build this out over the next couple of videos. So stay tuned, and I'll show you how to add properties, methods, and all the other things you would expect in a proper C# class.
- The history of C#
- Setting up your development environment in Visual Studio
- Declaring variables
- Working with strings
- Using mathematical operations
- Creating classes and properties
- Using expressions
- Using static, abstract, and virtual methods
- Building a user interface
- Extending classes
- Working with subclassed objects
- Using the object-oriented features of C#