What is Classes and Objects in c#

What is Classes and Objects in c#

In this article, you will learn about Classes and Objects in c#

 

A Class is like an object constructor, or a blueprint for creating objects.

 

A class is a user-defined blueprint or prototype from which objects are created, a class combines the fields and methods into a single unit. In C#, classes support polymorphism, inheritance and also provide the concept of derived classes and base classes.

 

class Car // class name Car
{
  string color = "blue";// variable name color
}

 

You can create multiple objects

 

class Car
{
  string color = "blue";
  static void Main(string[] args)
  {
    Car Obj1 = new Car();
    Car Obj2 = new Car();
    Car Obj3 = new Car();
    Console.WriteLine(Obj1.color);
    Console.WriteLine(Obj2.color);
    Console.WriteLine(Obj3.color);
  }
}

Create an Object

 

class Car 
{
  string color = "blue";

  static void Main(string[] args)
  {
    Car Obj = new Car();
    Console.WriteLine(Obj.color);
  }
}

Create an object called "Obj" and use it to print the value of color 

 

 

 

2021-10-03