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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s