C# Class And Objects With Example

cheezycode_class_and_objects_with_example



We have been using data types like int for integers, float for real numbers etc. But what if we want to store some complex data. For e.g. We are creating a website to sell cars. Now we want to store information about each car. Defining variables for each characteristic of a car (model number, type, color etc) is not an option because code becomes messy as we will store more than 5 characteristic of each car and there will be 'n' numbers of cars. This is where C# shines as it allows you to create complex types to store information.

You can create your own data type. What does that mean? Let's take one example - To store integer you use - int i = 10; Now I want to store information about Car - I want something like - Car myCar = information about a car; But wait, there is no such built-in type in C#. You can create your own type in C#. Creating new data types in C# is done with class keyword. Below is the code snippet to create new type or class -

class Car 
{  
    //Properties
    public int modelYear;
    public string carName;
    public string carType;
    public float price;
   
    //Methods
    public void DriveCar()
    {
       Console.WriteLine("Car is driving");
    }   
    public void ApplyBrake()
    {
       Console.WriteLine("Car is applying brakes");
    }   

}

We will learn about public keyword in other articles. Properties are characteristics of the object that are created using this class whereas Methods are behavior of the object. For e.g. Car object has DriveCar() behavior and ApplyBrake() behavior. Let's use this Car type as an example -

  
using System;
class Program
{
    static void Main(string[] args)
    {
        Car Mustang = new Car(); //Object 1        
        Mustang.carName = "Ford Mustang";
        Mustang.carType = "3.7L V6";
        Mustang.price = 24645F;
        Mustang.modelYear = 2017;
        Console.WriteLine("Car Name - {0}, Car Type - {1}, Year - {2}", Mustang.carName, Mustang.carType, Mustang.modelYear);

        Car Beetle = new Car(); //Object 2        
        Beetle.carName = "Volkswagen Beetle";
        Beetle.carType = "Beetle 1.8T";
        Beetle.price = 19795F;
        Beetle.modelYear = 2017;
        Console.WriteLine("Car Name - {0}, Car Type - {1}, Year - {2}", Beetle.carName, Beetle.carType, Beetle.modelYear);

        Console.ReadKey();
    }

}

class Car 
{  
    //Properties
    public int modelYear;
    public string carName;
    public string carType;
    public float price;
   
    //Methods
    public void DriveCar()
    {
       Console.WriteLine("Car is driving");
    }   
    public void ApplyBrake()
    {
       Console.WriteLine("Car is applying brakes");
    }   

}


In above code snippet, you can see that we have declared a new class Car. Execution starts from Main() method. This statement Car Mustang = new Car();  creates a new Car object named Mustang and then we assigned values to its properties using dot operator. We created another object named Beetle. They both are different objects and have different characteristics. After that you print the properties of each object using objectName.propertyName (dot operator).  

new keyword is used to create objects in C#. Now whenever you want to create objects of Car class, you will use - Car objectName = new Car(); This will create new object of Car and assigns it to objectName. This object is a unique Car object and has its own property values and behavior. Object and Instance are used interchangeably. Instantiation means creating a new object.

Difference between Class and Objects


Car is a concept where as Mustang and Beetle are 2 actual objects. Class is like a template that is used to create actual objects whereas objects are actual implementation of that template. Few other examples to clear the concept -

1. We are objects of Person class. We all can walk, talk etc. We all have different characteristics and we all do things differently. So Person here is a class and You and Me are actual objects.

2. You can take the example of chairs. Chair is class whereas we can have different types of chair objects like Dining Chair, Club Chair, Car Chair etc.

Refer these images as well -


cheezycode-car-blueprint

cheezycode-car-objects



Let's summarize -

1. You need to store complex data, for that you need data types.

2. You can create your own data type using Class Keyword.

3. By defining class, you create a blueprint.

4. You use this blueprint (class definition) to create objects using new keyword.

5. Each object has its own properties and behavior.

6. For e.g. Beetle and Mustang are both Car object created using Car Class. They both used the same template but they both have different properties and both behave differently. One accelerates more, other does not.

Let us know if you have any query or concerns. Happy Reading (refer full series here - C# Tutorial Index)



Comments

Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example