C# Object and Class

Classes and Objects

Imagine you have a box. This box is special because it can hold different things like toys, books, or anything you like. In programming, we have something similar called a “class.”

1. What is a Class?

A class is like a blueprint or plan for creating objects. Imagine you have a blueprint for building a house. It tells you what the house should look like and what rooms it should have. Similarly, a class tells the computer what properties and actions an object should have when it’s created.

Structure of Class
// Define a class
[access_modifier] class ClassName
{
    //Execution code like methods, fields , properties, constructor etc
}

2. What is an Object?

Now, imagine using that blueprint to create an actual car. That car you create is called an “object.”

An object is a real instance or copy of a class. It has unique properties (like the colour of the car) and behaviours (like driving or stopping).

Structure of Object
// Creating an object
ClassName objectName = new ClassName(parameters);
// Accessing fields
objectName.fieldName = value;
// Calling methods
objectName.MethodName();

Example: Car Class and Objects

using System;
class Car
{
     public string Colour;
     public int Speed;
   public void Drive(string colour , int speed)
  {
      Console.WriteLine(“The “+ colour +” car driving speed is ” + speed);
  }
     static void Main()
  {
      Car FirstCar = new Car();
      FirstCar.Colour = “Red”;
      FirstCar.Speed = 60;
      Car SecondCar = new Car();
      SecondCar.Colour = “Blue”;
      SecondCar.Speed = 200;

       FirstCar.Drive(FirstCar.Colour, FirstCar.Speed);
      SecondCar.Drive(SecondCar.Colour,SecondCar.Speed);
   }
}

Explanation

using System;

Define Simple class for the Car

class Car

Properties (attributes) of the car

public string Colour;
    public int Speed;

Methods (actions) the car can do

     public void Drive(string colour , int speed)
     {
          Console.WriteLine(“The “+ colour +” car driving speed is ” + speed);
     }
    static void Main()
    {

Let’s create objects (instances) of the Car class:

Car FirstCar = new Car();

Creating a new car object

FirstCar.Colour = “Red”;  

Setting the colour of the car

FirstCar.Speed = 60;     

Setting the speed of the car

Car SecondCar = new Car();

Creating another car object to create second  car

SecondCar.Colour = “Blue”; 

Setting colour

SecondCar.Speed = 200;     

Setting speed

Using the method of the car objects

FirstCar.Drive(FirstCar.Colour, FirstCar.Speed);

Output: The Red car driving speed is 60

SecondCar.Drive(SecondCar.Colour,SecondCar.Speed);

Output: The Blue car driving speed is 200

Course Video

Course Video In English

Task:

1. Create a class named Employee with the following properties:
string Name
int Id
double Salary
Create an object of the Employee class in the main method.
Assign values to the properties and print them to the console.

2. Create a class named Product with the following properties:
string ProductName
string Category
double Price
Create an object of the Product class in the main method.
Assign values to the properties and print them to the console.

3. Create a class named Student with the following properties:
string Name
int RollNumber
string Course
Create an object of the Student class in the main method.
Assign values to the properties and print them to the console.

4. Create a class named Car with the following properties:
string Make
string Model
int Year
Create an object of the Car class in the main method.
Assign values to the properties and print them to the console.

5. Create a class named Book with the following properties:
string Title
string Author
int Pages
Create an object of the Book class in the main method.
Assign values to the properties and print them to the console