Object Oriented Programming in C# – Constructors

Object oriented programming is an entirely new perspective on programming from procedural programming. It focuses on the manipulation of objects rather than a “top-down” approach. This in general makes object oriented programs easier to modify than procedural based programs. A class is a category of objects which defines common properties of all the objects that belong to it. An object is a self contained entity that consists of data and procedures to manipulate the data.

In the following snippet of a program, a class “Person” is initialized after the class “Program”. The variables defined within the class are hidden from the parent class unless they are made public. The “perOne” is in instance of a class which is an object.

prog

The “Firstname”, “LastName”, etc are properties of each object which are set to different strings.

A “constructor” is a method with the same name as the class, as shown by the line “Person perOne = new Person()”. The following shows the constructor for the class “Person”

const

If a property for “firstname” is not set, when a new object is created that property will default to “unknown” due to the constructor.