Constructors
Constructors are special methods that are invoked when an object of a class is created. Constructors are used to initialize objects and set the initial state of the class’s fields or properties. A constructor has the same name as the class and does not have a return type.
Types of Constructors
- Default Constructor: A constructor with no parameters. If no constructors are defined in a class, C# provides a default constructor automatically.
- Parameterized Constructor: A constructor that takes parameters, allowing you to pass values at the time of object creation.
- Static Constructor: A special constructor used to initialize static members of the class. It is called automatically before the first instance is created or any static members are accessed.
Example: Parameterized Constructor
using System;
class Book
{
 // Fields
 private string title;
 private string author;
 private int year;
  // Parameterized Constructor
 public Book(string title, string author, int year)
 {
   this.title = title;
   this.author = author;
   this.year = year;
 }
 // Properties
 public string Title
 {
   get { return title; }
   set { title = value; }
 }
 public string Author
 {
   get { return author; }
   set { author = value; }
 }
 public int Year
 {
   get { return year; }
   set { year = value; }
 }
  // Method to display book details
 public void DisplayDetails()
 {
   Console.WriteLine($"Title: {title}, Author: {author}, Year: {year}");
 }
}
class Program
{
 static void Main()
 {
   // Creating an object of the Book class using the parameterized constructor
   Book myBook = new Book("1985", "George Orwell", 1950);
    // Accessing properties
   Console.WriteLine($"Title: {myBook.Title}");
   Console.WriteLine($"Author: {myBook.Author}");
   Console.WriteLine($"Year: {myBook.Year}");
    // Modifying properties
   myBook.Year = 1951;
    // Calling a method
   myBook.DisplayDetails();
 }
}
Explanation of the Example
- Class Definition:
- We define a Book class with three private fields: title, author, and year.
- Parameterized Constructor:
- The Book class has a parameterized constructor that takes three parameters: title, author, and year. This constructor initializes the fields with the values passed to it.
- Properties:
- Properties Title, Author, and Year provide access to the private fields, allowing us to get and set their values.
- Method:
- The DisplayDetails method prints the book’s details to the console.
- Main Method:
- We create an object myBook of the Book class using the parameterized constructor and initialize it with specific values.
- We access and print the properties of myBook.
- We modify the Year property of myBook.
- We call the DisplayDetails method to display the book’s updated details.
Core Ideas
- Initialization: Constructors are responsible for initializing objects by assigning initial values to fields or properties. This ensures that the object is in a valid state upon creation.
- Overloading Constructors: C# allows for constructor overloading, which means you can define multiple constructors with different parameters within the same class. This offers flexibility in creating objects with various initializations.
- Access Modifiers: Constructors can implement different access modifiers—such as public, private, or protected—which control their visibility and determine how objects can be instantiated.
