Skip to main content

C# Object-Oriented Programming (OOPS ) Concepts with Code Examples

C# OOPS Concepts with Code Examples by Ziggy Rafiq




 

Overview

In this post, I am writing about one of the C# powerful features which is an object-oriented programming language that follows the principles of OOPS (Object-Oriented Programming System) concepts. These concepts are crucial in creating efficient and scalable applications. Let's look at the key OOPS concepts in C# with code examples:

Encapsulation

Encapsulation is the practice of hiding the internal details of an object and exposing only what is necessary. In C#, encapsulation can be achieved by using access modifiers (public, private, protected) to control the visibility of data members and methods. The Internal representation of an object is hidden from the view outside the definition of the object. Only the required information can be accessed whereas the rest of the data implementation is hidden.

Code Example

In the example below, the balance data member is declared private, which means it can only be accessed within the BankAccount class. The Deposit, Withdraw, and GetBalance methods provide controlled access to the balance data member.

Encapsulation is the practice of hiding the internal details of an object and exposing only what is necessary by Ziggy Rafiq


Abstraction

Abstraction is the process of identifying essential features of an object and ignoring the non-essential ones. In C#, abstraction can be achieved by using abstract classes (classes that cannot be instantiated but can be used as base classes) and interfaces (a collection of abstract methods that define a contract for implementing classes). It is a process of identifying the critical behaviors and data of an object and eliminating irrelevant details.

Code Example

In this example below, the Shape class is declared as abstract, which means it cannot be instantiated directly but can be used as a base class for other classes. The Rectangle and Circle classes derive from Shape and implement their own GetArea methods to calculate their respective areas.

Abstraction is the process of identifying essential features of an object and ignoring the non-essential ones by Ziggy Rafiq

Inheritance

Inheritance is a mechanism by which a class can inherit properties and behaviors from another class. The class that inherits is called a subclass or derived class, and the class that is inherited from is called a base class or superclass. In C#, inheritance is implemented using the colon ":" operator.
It is the ability to create new classes from another class. It is done by accessing, modifying and extending the behaviors of objects in the parent class.

Code Example

In this example below, the Employee class inherits from the Person class using the: operator. The Employee class defines its own data member (Salary) and member function (ShowSalary)

Inheritance is a mechanism by which a class can inherit properties and behaviors from another class by Ziggy Rafiq


Polymorphism

The name means, one name, many forms. It is achieved by having multiple methods with the same name but different implementations. Polymorphism is the ability of objects to take on different forms. In C#, polymorphism can be achieved through method overloading (defining multiple methods with the same name but different parameters) and method overriding (defining a method in a subclass with the same name and parameters as a method in its superclass).

Code Example

In the example below, the Animal class defines a virtual method (MakeSound) that can be overridden by subclasses. The Dog and Cat classes override the MakeSound method with their own implementations.

The name means, one name, many forms. It is achieved by having multiple methods with the same name but different implementations by Ziggy Rafiq

Class

A class is a blueprint or a template that defines the properties and behaviors of an object. It contains data members (variables) and member functions (methods) that operate on those variables.

 

Code Example

In the example below, the Person class defines two data members (Name and Age) and a member function (SayHello) that operate on those data members.

A class is a blueprint or a template that defines the properties and behaviors of an object.  Ziggy Rafiq

Object

An object is an instance of a class. It has its own state (values of its variables) and behaviour (methods).

Code Example

In this example below, an object of the Person class is created and its data members (Name and Age) are set. Then, the SayHello method is called on the object.

object of the Person class is created and its data members (Name and Age) are set. Then, the SayHello method is called on the object. by Ziggy Rafiq

These OOP concepts provide a structured and modular approach to programming and can make code more efficient, maintainable, and easier to understand.


Comments

Most Viewed Ziggy Rafiq Blog Posts

A Complete Guide to Using GUIDs in C# with Code Examples

  Overview In this post, we are looking at GUIDs (Globally Unique Identifiers), which are widely used in C# for generating unique identifiers for objects, entities, and resources in a system. In this post, we'll explore the basics of GUIDs in C#, their advantages, and how to convert strings to GUIDs. In this post, we have used Guid Generator to create the GUID from the following URL Address https://guidgenerator.com/ What is GUID GUID (Globally Unique Identifier) in C# is a 128-bit value that is used to identify objects, entities, or resources in a unique manner across different systems and applications. It is also known as UUID (Universally Unique Identifier) in some other programming languages.   GUIDs are generated using a combination of unique factors such as the MAC address of the network adapter, the current time and date, and a random number. The resulting GUID is a string of 32 hexadecimal digits separated by hyphens, such as "b86f2096-237a-4059-8329-1bbcea72769b...

Primitives Data Types and None-Primitives Data Types in C# with Code Examples

  Overview I wrote this post to provide an explanation of primitive and non-primitive data types in C#. C# is a strongly typed programming language, where each variable and expression must have a specific data type. C# data types are categorized into two primary groups: primitive data types and non-primitive data types. Primitive data types are the simplest data types available in programming languages. They are typically pre-defined data types and can represent a single value, such as a boolean value, character, or integer. Examples of primitive data types include int, char, float, double, and boolean, which are common in programming languages like C++, C, and Java. Non-primitive data types are also referred to as composite data types or reference data types. They are constructed from primitive data types and are more complex than primitive data types. Non-primitive data types can hold multiple values and allow for the creation of more intricate data structures like tables, li...

How to Truncate String in C#

  Overview In today’s post, I am writing about one of the fundamental aspects of C# and as programmers, we should use this approach I have realised throughout my life as a software engineer that not everyone knows the most basic approaches or how to use them efficiently. Truncating a string in C# means reducing the length of a string to a specified number of characters, typically by removing characters from the end of the string. This is often done when a string is too long to be displayed or used in a specific context and needs to be shortened to fit. Ley says if we have a string that is 100 characters long, but we only want to display the first 50 characters, we will truncate the string to a length of 50. There are several ways to truncate a string in C#, including using the Substring method, StringBuilder, and LINQ. A simple example of this is below, we're creating a longString variable with a long string value. We're also defining a maxLength variable with a value of 20...