Introduction to OOP in C#: Classes - Page 1
       by kirupa  |  5 January 2007

In the real world, you categorize and sub-categorize your environment based on the objects that can be found in it. For example, you have a chair which can be categorized as furniture. Your refrigerator can be categorized as an appliance. There are many such examples where a specific object is classified by a more general category. 

When it comes to writing code, you can represent things as objects also. This style of programming is known object oriented programming (OOP) where we extend our idea of objects from the real world and represent them using code.

On the Menu
In this tutorial, I will provide you with an introduction to OOP by covering classes in C# and some of the interesting things to be on the lookout out for. Since this will be a long (but fun!) tutorial, I have divided the content into the following smaller sections for easier digestion:

  1. Introducing Classes and Objects
  2. Setting Up your Project
  3. Defining a Class
    1. Public and Private Modifiers
    2. Constructors
  4. Methods (Functions)
    1. Instance Methods
    2. Static Methods
  5. AutoComplete in Visual Studio
  6. Conclusion

Introducing Classes and Objects
Let's step away from the world of computers and think about how objects and classes relate to the real world, and since I am a big fan of outer space, I will use an intergalactic theme throughout this tutorial.

In our solar system, you have planets. For something to be a planet, it must meet the following criteria:

  • Orbits around the sun.
  • Has enough mass to maintain a stable shape.
  • Does not shine with its own light.

There are a few more criteria, but you get the basic idea of what characteristics something must possess in order to be considered a planet. Now that you know what constitute a planet represents, you can then associate real world things that would fall under the planet category. There are a handful of planets in our solar system, and one such planet, Earth, is a pretty important one! The reason Earth and the rest are considered planets is because they orbit the sun, have mass, and do not produce their own light.

Ok, now let's link the planet example to our goal of understanding classes. A class would be similar to the definition of planets, for classes set the guidelines and specify the characteristics necessary for something to be associated with it. The individual planets themselves - Mercury, Mars, Earth, etc. - would be considered objects or instances because they inherit their unique characteristics from a class called planet. Another way of saying it would be that these objects are of type planet.

To recap, in order for something to be a planet, it must posses all the characteristics as defined by your Planet category. In order for an object to be part of a class, the object must incorporate all of the characteristics specified as being associated with the class. There are more nitpicky details, but for now, we'll play it by ear and learn those details along the way.

Onwards to the next page!

1 | 2 | 3 | 4

SUPPORTERS:'s fast and reliable hosting provided by Media Temple.