Learn the basics of creating a class in C#. Classes allow you to encapsulate code logic making them a cornerstone of OOP development.
- [Instructor] Let's take a look at how encapsulation works. Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object. In order to do this, we're going to create a new project in Visual Studio. We're going to use the Console Application template, and let's name this OOPExamples. Since this is a simple project, I've also unchecked the create directory for solution since we'll keep all of the code at the root of our project.
When you've picked a location, where to save your project, go ahead and click the OK button. Here, you'll be presented with a standard class in order to run our program. Let's go ahead and right-click on our project in the solution and select Add, New Item. Here, we're going to create a class. Let's call this class Computer. Visual Studio will automatically create a new class template for us.
Here you'll see it includes some commonly used libraries as well as provides a namespace and the class itself. Let's take a look at how we can implement an instance of this class. We'll go back into our program file and in our static main method let's add a new variable called var comp = new Computer. When we talk about encapsulation, what this really means is that all the properties and things that make a computer up will exist within the Computer class.
As you can see, our Program class is a different class that's able to still get a reference to our Computer and create new copies of it. By default, Visual Studio automatically includes our classes in the same namespace. A namespace is a way to better organize and group classes within a library. Namespaces also help avoid conflicts if you use the same code in different projects. To show a little bit better about how the computer acts on its own inside of its own namespace, let's go ahead and delete the namespace from our program.
You can also select the Program class, and while holding down Shift and hit Tab reformat it to move it over. As you can see, Computer is showing a red underline. This let's us know that we can no longer access the Computer class since we're no longer in the same namespace. The way that we can fix this is by including the Computer's namespace. Just like you see at the top of our class, let's use the using keyword and type in OOPExamples with a semicolon.
You'll see that the using statement is no longer grayed out, because we actually have a reference to it within our main code. We can go ahead and delete the references to the other libraries we're not using at this time. Now that we have a reference to our Computer, let's take a look at what the Computer actually is. Let's go ahead and output the Computer's type to the console. Here, we can type Console.WriteLine, and we'll say Computer Type with a dash, and we'll add a reference to the Computer and call a method on it, GetType.
Now, as you can see, Console now has a red underline as well, and that's because we also removed the using System namespace. In order to get access to the Console classes, let's go ahead and type using System, and now we'll have reference to outputting text to our Console. The last thing we need to do before running our code is force the console window to stay open. Let's do this by typing Console.Read.
Let's go ahead and now run our program and see what gets outputted to the console. As you can see, we now have a reference to our Computer type, and it's outputting that our Computer type is an OOPExamples.Computer. OOPExamples is our namespace, and Computer is the class itself. We can also test whether a variable is a particular type of class.
Let's create a new variable, and we're going to call this var isComputer, and we're going to set it equal to comp is and a reference to the Computer class itself. Now, let's add a new Console output, and here we're going to write Is computer, and we'll include a reference to the isComputer variable we just created.
Now, let's go ahead and save our code and rerun our program. As you can see, we're now directly comparing a variable to a type of class. We can use this in order to determine what typed classes are without having to explicitly get their type. At this point, we've learned the basics of how to create a class, how to use namespaces to separate classes to avoid conflicts in our code, and also to determine if a variable is particular type of class.
- Creating classes, fields, and access modifiers
- Adding properties and methods
- Working with constructors
- Extending and modifying classes
- Overriding properties and methods
- Working with composite classes
- Considering interfaces