C# IntelliBits: Object Oriented Programming Concepts in C#

In this installment of C# IntelliBits, we briefly look at the history and characteristics of object oriented programming in C#. (Check out my other columns for more C# tips and C# 5.0 tips.)

Object oriented programming (OOP) is an approach to software development in which the structure of the software is based on objects interacting with each other to accomplish a task. This interaction takes the form of messages passing back and forth between the objects. In response to a message, an object can perform an action or method.

A Brief History of Object Oriented Programming

OOP concepts surfaced in the mid-1960s with a programming language called Simula and further evolved in the 1970s with the advent of Smalltalk. Although software developers did not overwhelmingly embrace these early advances in OOP languages, object-oriented methodologies continued to evolve.

In the mid-1980s there was a resurgence of interest in object-oriented methodologies. Specifically, OOP languages such as C++ and Eifle became popular with mainstream computer programmers. OOP continued to grow in popularity in the 1990s, most notably with the advent of Java and the huge following it attracted. And in 2002, in conjunction with the release of the .NET Framework, Microsoft introduced a new OOP language C#.

Object Oriented Programming Concepts


In object oriented programming terms, an object is a structure for incorporating data and the procedures for working with that data. For example, if you were interested in maintaining data associated with company employees’ information, you would create an employee object that is responsible for maintaining and using the data pertaining to those employees.


A class is a blueprint that defines the attributes and behaviors of the objects that are created as instances of the class. The properties and methods of the objects used in OOP are defined through classes:

//Code snippet of an Employee class and object

class Employee  


Employee  hrEmployee=new Employee ();

According to the above sample, we can say that the Employee object, named hrEmployee, has been created out of the Employee class.


Encapsulation is the ability to hide unnecessary implementation details from the object user. For example, assume you are using a class named DatabaseReader, which has two primary methods named Open() and Close():

// Assume this class encapsulates the details of opening and closing a db.
DatabaseReader dbReader = new DatabaseReader();

// Do something with data file and close the file.

The fictitious DatabaseReader class encapsulates the inner details of locating, loading, manipulating, and closing the data file.


Abstraction is the process that involves recognizing and focusing on the important characteristics of a situation or object, and filtering out or ignoring all the unessential details. Here’s an example:

using System;

namespace abstractSample
      //Creating an Abstract Class
      abstract class absClass
            //A Non abstract method
            public int AddTwoNumbers(int Number1, int Number2)
                return Number1 + Number2;

            //An abstract method, to be
            //overridden in derived class
            public abstract int MultiplyTwoNumbers(int Number1, int Number2);

      //A Child Class of absClass
      class absDerived:absClass
            static void Main(string[] args)
               //You can create an
               //instance of the derived class

               absDerived calculate = new absDerived();
               int added = calculate.AddTwoNumbers(10,20);
               int multiplied = calculate.MultiplyTwoNumbers(10,20);
               Console.WriteLine("Added : {0}, 
                       Multiplied : {1}", added, multiplied);

            //using override keyword,
            //implementing the abstract method
 public override int MultiplyTwoNumbers(int Number1, int Number2)
                return Number1 * Number2;


Inheritance allows the creation of hierarchical classifications. Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it.

In the language of C#, a class that is inherited is called a base class. The class that does the inheriting is called a derived class. Therefore, a derived class is a specialized version of a base class. It inherits all of the variables, methods, properties, and indexers defined by the base class and adds its own unique elements. Here’s an example:

public class Person
  private string name;

  // public Name property syntax not shown.

  public virtual string GetDescription() {
    return name;
    // e.g., "Bobby Fischer"

Later on, we decide to specialize Person by creating two derived classes—Professor and Student—which each add a few fields as well as overriding the GetDescription method to take advantage of their newly added fields, as follows:

public class Student : Person
  // We add two fields.
  string major;
  int id; // a unique Student ID number

  // Override this method as inherited from Person.
  public override string GetDescription() {
    return Name + " [" + major + "; " + id + "]";
    // e.g., "Steve Jobs [Math; 10273]"
public class Professor : Person
  // We add two fields; note that one has the same name, but a
  // different data type, as a field of Student.
  string title;
  string id; // a unique Employee ID number

  // Override this method as inherited from Person.
  public override string GetDescription() {
    return Name + " [" + title + "; " + id + "]";
    // e.g., "Bill Gates [Chairman; A723]"


In the C# object-oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions.'

Polymorphism provides a way for a subclass to define its own version of a method defined by its base class, using the process termed method overriding. To retrofit your current design, you need to understand the meaning of the virtual and override keywords. If a base class wants to define a method that may be (but does not have to be) overridden by a subclass, it must mark the method with the virtual keyword:

partial class Employee
  // This method can now be "overridden" by a derived class.
  public virtual void GiveBonus(float amount)
    Pay += amount;

When a subclass wants to change the implementation details of a virtual method, it does so using the override keyword. For example, the SalesPerson and Manager could override GiveBonus() as follows (assume that PTSalesPerson will not override GiveBonus() and, therefore, simply inherits the version defined by SalesPerson):

class SalesPerson : Employee
  // A salesperson's bonus is influenced by the number of sales.
  public override void GiveBonus(float amount)
    int salesBonus = 0;
    if (SalesNumber >= 0 && SalesNumber <= 100)
      salesBonus = 10;
      if (SalesNumber >= 101 && SalesNumber <= 200)
        salesBonus = 15;
        salesBonus = 20;
    base.GiveBonus(amount * salesBonus);

In this installment of IntelliBits we covered the definitions of the four main concepts of object oriented programming in C# and provided a brief history of the advent of OOP.

I hope you have enjoyed this installment of C# IntelliBits.

Thanks for reading!