Utilizor
Contact Us

C# OOP

Introduction to Object-Oriented Programming.

C# What is OOP?

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.

Object-oriented programming has several advantages over procedural programming:

  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP helps to keep the C# code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug
  • OOP makes it possible to create full reusable applications with less code and shorter development time

Tip: The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating it.

Examples

Code Reuse (Method)

DRY concept simple example.

using System;

class Program
{
  // A simple example of code organization, pre-OOP concept
  static void PrintMessage() {
      Console.WriteLine("Hello World");
  }

  static void Main(string[] args)
  {
    PrintMessage();
    PrintMessage();
  }
}

Class Structure

Encapsulating data and behavior.

using System;

// A Class organizes data and behavior
class Person {
    public string name;
    public void Introduce() {
        Console.WriteLine("Hi, I am " + name);
    }
}

class Program {
    static void Main() {
        Person p = new Person();
        p.name = "John";
        p.Introduce();
    }
}