Object and Class

In C#, objects and classes are core concepts of object-oriented programming (OOP). A class is a blueprint for creating objects, which are instances of that class. Classes encapsulate data and behavior, helping to organize code into modular and reusable components.

Classes

A class is a logical entity that defines a type and what it can do. A class can be thought of as a blueprint that specifies the properties and methods of a type. To declare a class in C#, use the keyword class followed by the access level and a custom name, and end with curly braces.

Objects

An object is a physical entity that is created based on a class’s blueprint. An object is a block of memory that has been configured to match the class’s definition. To create an object of a class in C#, use the new keyword followed by the name of the class.

Basics of Classes

A class in C# is defined using the class keyword followed by the class name. Inside the class, you can define fields, properties, methods, and constructors.
  • Fields: Variables that store the data for the class.
  • Properties: Provide controlled access to the fields.
  • Methods: Functions that define the behavior of the class.
  • Constructors: Special methods used to initialize objects.

Example

using System;

class Person
{
  // Fields
  private string firstName;
  private string lastName;
  private int age;

    // Constructor
  public Person(string firstName, string lastName, int age)
  {
      this.firstName = firstName;
      this.lastName = lastName;
      this.age = age;
  }

 
// Properties
  public string FirstName
  {
      get { return firstName; }
      set { firstName = value; }
  }
  public string LastName
  {
      get { return lastName; }
      set { lastName = value; }
  }
  public int Age
  {
      get { return age; }
      set { age = value; }
  }

    // Method
  public void DisplayDetails()
  {
      Console.WriteLine($"Name: {firstName} {lastName}, Age: {age}");
  }
}

class
Program
{
  static void Main()
  {
      // Creating an object of the Person class
      Person person1 = new Person("John", "Doe", 30);

        // Accessing properties
      Console.WriteLine($"First Name: {person1.FirstName}");
      Console.WriteLine($"Last Name: {person1.LastName}");
      Console.WriteLine($"Age: {person1.Age}");

        // Modifying properties
      person1.Age = 31;
      // Calling a method
      person1.DisplayDetails();
  }
}

Explanation of the Example

  1. Class Definition:
    • We define a Person class with three private fields: firstName, lastName, and age.
    • A constructor initializes these fields when an object of the Person class is created.
  2. Properties:
    • Properties FirstName, LastName, and Age provide access to the private fields. They use get and set accessors to read and modify the field values.
  3. Method:
    • The DisplayDetails method prints the person’s details to the console.
  4. Main Method:
    • We create an object person1 of the Person class and initialize it with specific values.
    • We access and print the properties of person1.
    • We modify the Age property of person1.
    • We call the DisplayDetails method to display the person’s updated details.

Core Ideas

  1. Encapsulation:
    • The Person class encapsulates the fields firstName, lastName, and age. These fields are private, meaning they cannot be accessed directly from outside the class.
    • Access to these fields is provided through public properties, ensuring controlled access and modification.
  2. Object Creation:
    • The new keyword is used to create an instance (object) of the Person class. The constructor is called to initialize the object’s fields.
  3. Methods:
    • Methods define behavior that objects of the class can perform. In this example, the DisplayDetails method provides a way to display the person’s details.
Post a comment

Leave a Comment

Scroll to Top