APM for All! NEW Retrace consumption pricing starts at $9.99 per month! Learn more

OOP Concepts in C#: Code Examples and How to Create a Class

By: Alexandra
 |  August 8, 2017

Object oriented programming (OOP) is a programming structure where programs are organized around objects as opposed to action and logic. This is essentially a design philosophy that uses a different set of programming languages such as C#. Understanding OOP concepts can help make decisions about how you should design an application and what language to use.

Everything in OOP is placed together as self-sustainable “objects.” An object is a combination of variables, functions, and data that performs a set of related activities. When the object performs those activities, it defines the object’s behavior. In addition, an object is an instance of a class. Furthermore, C# offers full support for OOP including inheritance, encapsulation, abstraction, and polymorphism:

  • Encapsulation is when a group of related methods, properties, and other members are treated as a single object.
  • Inheritance is the ability to receive (“inherit”) methods and properties from an existing class.
  • Polymorphism is when each class implements the same methods in varying ways, but you can still have several classes that can be utilized interchangeably.
  • Abstraction is the process by which a developer hides everything other than the relevant data about an object in order to simplify and increase efficiency.

We’ll discuss each of these concepts in more detail in this post.

What is an Object?

Objects are instances of classes. In other words, an instance of a class is an object defined by that particular class. Creating a new instance, or an object, is called instantiation. This is how you define a class:


class SampleClass



A light version of classes in C# is called structures. These are beneficial when you want to create a large array of objects but don’t want to overwhelm your available memory. A class is made up of three things:

  • A name
  • Operations
  • Attributes

Objects are created from a class blueprint, which defines the data and behavior of all instances of that type.

public class Student



Here’s another example:

Class Example


/* fields,






Here’s an example of an object:

Example exmplObject = new Example();

In memory, you can create an object using the “new” keyword. In C#, value types refer to other data type variables while objects are reference types. Moreover, other value types are stored in the stack while objects are stored in the heap.

Keep in mind that everything in C# is a class. An object is a section of memory that has been configured according to the class blueprint. Every instance, or object, of a particular class has access to several methods and properties of that class.

Data Abstraction

This provides essential features without describing any background details. Abstraction is important because it can hide unnecessary details from reference objects to names. It is also necessary for the construction of programs. Instead of showing how an object is represented or how it works, it focuses on what an object does. Therefore, data abstraction is often used for managing large and complex programs.


This binds the member function and data member into a single class. This also allows for abstraction. Within OOP, encapsulation can be achieved through creating classes. Those classes then display public methods and properties. The name encapsulation comes from the fact that this class encapsulates the set of methods, properties, and attributes of its functionalities to other classes.


This is the ability of an object to perform in a wide variety of ways. There are two types:

1. Dynamic polymorphism (runtime time). You can obtain this type through executing function overriding.

2. Static polymorphism (compile time). You can achieve static polymorphism through function overloading and operator overloading.

Within OOP, polymorphism can be achieved using many techniques including:

  • Method overloading (defining several methods at the same time)
  • Method overriding (this allows a subclass to override a specific implementation of a method already issued by one of its super-classes)
  • Operator overloading (some or all of the operators are handled has polymorphic functions with different behaviors depending on the types of its arguments)


Through inheritance, a class can “inherit” the characteristics of another general class. To illustrate, dogs are believed to be the descendants of wolves. All dogs have four paws and can hunt their prey. This function can be coded into the Wolf class. All of its descendants can use it. Inheritance is also an is-kind-of relationship. For instance, a golden retriever is a kind of animal. Here’s an example:

class BaseClass



class DerivedClass : BaseClass



How to Create a Class

All you have to do to create a class is to add a class file to your project. The next step is to right-click on your project within the solution explorer and click Add, then choose New Item. You’ll see a new window. On the left side of the window, click Class in the Code template. Select a name for your class and click Add. It looks like this:

namespace OOPLearning


class Cat




A method is an action an object can execute. In most instances, you can declare a method within a class definition. Yet, C# supports extension methods that let you add methods to an existing class outside the definition of a class.

Here’s an example of a simple C# program called “Hello World” from C# Corner that illustrates many of OOP fundamentals:

using System;

namespace oops

//class definition
public class SimpleHelloWorld
//Entry point of the program
static void Main(string[] args)
//print Hello world"
Console.WriteLine("Hello World!");

In order for a programming language to be object-oriented, it must have the ability to work with classes and objects. Moreover, it must use the fundamental object-oriented principles of abstraction, inheritance, polymorphism, and encapsulation.

Additional Resources and Tutorials

For more information on OOP concepts in C# and how you can best utilize OOP, visit the following resources and tutorials:


Going into 2018, we expect C# and .NET Core to be huge, so it’s worth learning the fundamentals if you’re not already familiar. Check out some of our other posts for more basics and advanced concepts in C#, such as throwing C# exceptions, how to handle C# exceptions, catching them and finding application errors, and other tips.

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More

Want to contribute to the Stackify blog?

If you would like to be a guest contributor to the Stackify blog please reach out to [email protected]