Classes and Objects in C# .NET


C# .NET is an Object Oriented programming language. Objects are created from Classes, which you've already used a lot. The Windows Form itself is a Class, and when you run your programme, you are creating Objects: a Form object, a button object, a Textbox object, etc. In this section, you'll learn how to write your own Classes, and turn them in to Objects. First, and explanation

What is a Class?

A Class is simply a chuck of code that does a particular job. You might have a class that handles all your database work, for example, or one that does error checking on Textboxes.

The idea is that you can reuse this code (Class) whenever you need it, or when you need it in other projects. It saves you from having to write the same thing over and over again.

Think of a Class as a recipe. If you had a recipe for a delicious Banana Cake, the recipe will tell you what you need to do to make the cake. But it's not the cake itself. It's the instructions for the cake. If you have the recipe, you can make Banana Cakes whenever you need them.

What is an Object?

An Object is the thing that the recipe makes - The Banana Cake itself. You do all the coding in a Class (recipe), and then instruct C# to make an object (a Banana Cake). The two processes are different.

But all you are really doing with Classes and Objects is trying to separate code into chunks, so that it can be reused. When the code is being used, it's an object. But the object is created from your code (Class).

Let's get some practice.