LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Classes and Objects in C#

Individual employees become objects. This concept is part of Object-Oriented Programming (OOP), a programming approach that organises applications around objects. We use it to build reusable and maintainable applications.

1Understanding Classes and Objects

Hi friends, in this article we will learn about classes and objects in C#. A class is a blueprint that defines data and behaviour. An object is an actual instance created from that class. For example, an employee class can define employee details and actions. Individual employees become objects. This concept is part of Object-Oriented Programming (OOP), a programming approach that organises applications around objects. We use it to build reusable and maintainable applications.

2Think About a Car

Imagine a car manufacturing company. Every car follows a common design. The design defines properties such as colour, model and speed. It also defines actions such as Start and Stop.

This design is the class. When the company manufactures individual cars using that design, each car is an object. Each object can have its own values while sharing the same structure and behaviour.

3Explore the Main Building Blocks

1. Class

A class is a blueprint. It groups variables and methods that belong together.

2. Object

An object is an instance of a class. We create it using the new keyword.

3. Properties

Properties represent an object's data. For example, Name and Brand describe a car.

4. Methods

Methods define what an object can do. For example, Start() represents a car action.

Concept Car illustration
Class Car design
Object Individual car
Property Colour, Brand
Method Start, Stop
Remember One class can create multiple objects. Each object can store different property values.

4Create Your First Class

Let's create a Car class with two properties and one method. Then we will create two objects with different values.

Program.csC#
using System;

class Car
{
    public string Brand { get; set; }
    public string Color { get; set; }

    public void Start()
    {
        Console.WriteLine(Brand + " is starting.");
    }
}

class Program
{
    static void Main()
    {
        Car car1 = new Car();
        car1.Brand = "Toyota";
        car1.Color = "White";

        Car car2 = new Car();
        car2.Brand = "Honda";
        car2.Color = "Black";

        Console.WriteLine(car1.Brand + " - " + car1.Color);
        car1.Start();
        Console.WriteLine(car2.Brand + " - " + car2.Color);
        car2.Start();
    }
}
Pro Tip Use meaningful class and property names. It makes your code easier for other developers to understand.

5See What the Program Prints

Both objects use the same Car class, but each object has its own brand and colour.

Output
Toyota - White
Toyota is starting.
Honda - Black
Honda is starting.
Common Mistake A class is only a blueprint. Create an object before accessing its instance properties or methods.

6Watch Objects Come to Life

Follow how two objects are created from one class and perform the same method with different data.

● ● ●Program.cs - Visual Studio Code
▸ Program.cs
▣
⌕
⑂
⚙
 
PROBLEMS  OUTPUT  DEBUG CONSOLE  TERMINAL
 
 
 
Ln 25, Col 9  UTF-8  C#

7What Have We Learned?

Classes define the structure and behaviour of objects. Objects hold their own data and can execute class methods. This is one of the most important foundations of C# application development.

  • A class is a blueprint.
  • An object is an instance of a class.
  • Properties store data and methods define behaviour.

8Questions Interviewers May Ask

Q1Easy

What is a class in C#?

A class is a blueprint that defines the data and behaviour of objects. It can contain properties, fields, methods and constructors.

Q2Easy

What is an object?

An object is an instance of a class. It has its own state and can use the behaviours defined by the class.

Q3Easy

How do you create an object in C#?

Use the new keyword followed by the class name and constructor, such as Car car = new Car();.

Q4Medium

Can we create multiple objects from one class?

Yes. We can create many objects from one class. Each object can maintain its own instance property values.

Q5Medium

What is the difference between a field and a property?

A field is a variable declared inside a class. A property provides controlled access to data, commonly using get and set accessors.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.