C# Classes & Objects: Foundations for Robust OOP
C# Classes & Objects: Foundations for Robust OOP
In this tutorial, you will learn about the concept of classes and objects in C# with the help of examples.
C# is an object‑oriented language that structures applications around objects. In OOP, we solve complex problems by breaking them into reusable, self‑contained objects.
To work with objects, we need to perform the following activities:
- Create a class
- Instantiate objects from that class
C# Class
A class is the blueprint for an object. Think of it as a house plan: it defines rooms, doors, windows, and structural rules. An actual house built from that plan is an object.
Multiple houses can share the same plan, just as many objects can be created from the same class.
Create a class in C#
class ClassName
{
}
Here we have defined a class named ClassName. A class can contain
- Fields – variables that hold data
- Methods – functions that perform actions
Example:
class Dog
{
// Field
string breed;
// Method
public void Bark()
{
// Implementation
}
}
In the example above:
- Dog – class name
- breed – field
- Bark() – method
Note: In C#, members of a class include both fields and methods.
C# Objects
An object is an instance of a class. For example, the class Dog can produce objects such as a Bulldog, German Shepherd, or Pug.
Creating an Object of a class
ClassName obj = new ClassName();
Here new creates an object; obj is its name. For instance:
Dog bullDog = new Dog();
The bullDog object can now access the fields and methods defined in Dog.
Access Class Members Using an Object
Access members with the dot (.) operator:
using System;
namespace ClassObject
{
class Dog
{
string breed;
public void Bark()
{
Console.WriteLine("Bark Bark !!");
}
static void Main(string[] args)
{
// create Dog object
Dog bullDog = new Dog();
// set breed
bullDog.breed = "Bull Dog";
Console.WriteLine(bullDog.breed);
// invoke method
bullDog.Bark();
Console.ReadLine();
}
}
}
Output
Bull Dog Bark Bark !!
Notice the use of bullDog and the dot operator to access breed and Bark().
Creating Multiple Objects of a Class
Instantiate several objects from the same class, each with its own data:
using System;
namespace ClassObject
{
class Employee
{
string department;
static void Main(string[] args)
{
// first employee
Employee sheeran = new Employee();
sheeran.department = "Development";
Console.WriteLine("Sheeran: " + sheeran.department);
// second employee
Employee taylor = new Employee();
taylor.department = "Content Writing";
Console.WriteLine("Taylor: " + taylor.department);
Console.ReadLine();
}
}
}
Output
Sheeran: Development Taylor: Content Writing
Each object holds its own department value.
Creating Objects in a Different Class
Objects can be created in other classes. For example:
using System;
namespace ClassObject
{
class Employee
{
public string name;
public void Work(string work)
{
Console.WriteLine("Work: " + work);
}
}
class EmployeeDrive
{
static void Main(string[] args)
{
// create Employee object
Employee e1 = new Employee();
Console.WriteLine("Employee 1");
// set name
e1.name = "Gloria";
Console.WriteLine("Name: " + e1.name);
// call method
e1.Work("Coding");
Console.ReadLine();
}
}
}
Output
Employee 1 Name: Gloria Work: Coding
Here EmployeeDrive creates an Employee object. Public members allow cross‑class access. Learn more about access modifiers in the official docs.
Why Objects and Classes?
Classes and objects enable modular design. For a game with many enemies, a single Enemy class can define health, ammo, Shoot(), and Run(). Each enemy instance then maintains its own state while sharing behavior.
By modeling code around objects, we reduce complexity, improve maintainability, and promote reuse.
C Language
- Mastering C# Abstract Classes & Methods: A Practical Guide
- Understanding C# Partial Classes and Methods
- Master sealed classes and methods in C#—learn when to use them, see clear examples, and boost code safety and performance.
- C++ Classes & Objects: A Practical Guide to Object‑Oriented Programming
- Mastering Java Inheritance: Concepts, Types, and Practical Examples
- Java Singleton Pattern Explained: How to Implement and Use It Safely
- Mastering Java Reflection: Inspecting Classes, Methods, and Fields at Runtime
- C++ Classes & Objects: A Practical Guide with Code Examples
- Master Java: Understanding Objects, Classes, and Core OOP Concepts
- Java Object Serialization: Persisting and Restoring Objects