Classes and Objects in C#: Constructors, Properties & this Keyword

1. Classes and Objects Overview

Q: What are classes and objects in C#?

Syntax:

class ClassName {
    // Fields, properties, methods
}

Q: How do classes and objects support OOP in C#?

Classes and objects enable OOP principles like:

Q: How do classes in C# differ from C/C++?

2. Defining Classes and Creating Objects

Q: How do you define a class in C#?

Define a class using the class keyword, specifying fields, properties, methods, and access modifiers.

Syntax:

class ClassName {
    public type FieldName; // Field
    public type PropertyName { get; set; } // Auto-implemented property
    public returnType MethodName() { /* Code */ }
}

Q: How do you create an object in C#?

Create an object using the new keyword, which allocates memory on the heap and calls a constructor.

Syntax:

ClassName obj = new ClassName();

Q: What are properties in C# classes?

Properties are special methods that provide controlled access to fields, using get and set accessors. Auto-implemented properties ({ get; set; }) simplify code.

Example:

public string Name { get; set; } // Auto-implemented property

Q: Can you give an example of defining a class and creating objects in C#?

using System;

namespace ClassesAndObjects
{
    class Person
    {
        // Fields
        private int age;

        // Properties
        public string Name { get; set; }
        public int Age
        {
            get { return age; }
            set { age = value >= 0 ? value : 0; } // Validation
        }

        // Method
        public void Introduce()
        {
            Console.WriteLine($"Hi, I'm {Name}, {Age} years old.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Creating objects
            Person person1 = new Person();
            person1.Name = "Krishna";
            person1.Age = 25;

            Person person2 = new Person { Name = "Kristal", Age = 30 }; // Object initializer

            // Calling method
            person1.Introduce();
            person2.Introduce();
        }
    }
}

Output:

Hi, I'm Krishna, 25 years old.
Hi, I'm Kristal, 30 years old.

3. Constructors and Destructors

Q: What is a constructor in C#?

A constructor is a special method called when an object is created to initialize its state. It has the same name as the class, no return type, and can be overloaded. Types include:

Q: What is a destructor in C#?

A destructor is a method called when an object is garbage-collected to clean up resources. It uses the ~ClassName syntax, has no parameters, and is rarely used due to C#’s garbage collector. Instead, implement IDisposable for explicit resource cleanup.

Q: How do you implement IDisposable for resource cleanup?

Use the IDisposable interface with a Dispose method to release unmanaged resources (e.g., file handles). The dispose pattern includes a finalizer (~destructor) as a fallback.

Q: Can you give an example of constructors and destructors in C#?

using System;

namespace ConstructorsAndDestructors
{
    class Resource : IDisposable
    {
        private bool disposed = false;

        // Default constructor
        public Resource()
        {
            Console.WriteLine("Default constructor called");
        }

        // Parameterized constructor
        public Resource(string name)
        {
            Name = name;
            Console.WriteLine($"Parameterized constructor: {Name}");
        }

        // Static constructor
        static Resource()
        {
            Console.WriteLine("Static constructor called");
        }

        public string Name { get; set; }

        // Destructor (finalizer)
        ~Resource()
        {
            Dispose(false);
        }

        // IDisposable implementation
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this); // Prevent finalizer from running
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // Dispose managed resources
                    Console.WriteLine("Disposing managed resources");
                }
                // Dispose unmanaged resources
                Console.WriteLine("Disposing unmanaged resources");
                disposed = true;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Using default constructor
            Resource res1 = new Resource();
            res1.Name = "Resource1";
            res1.Dispose(); // Explicit cleanup

            // Using parameterized constructor
            using (Resource res2 = new Resource("Resource2"))
            {
                // Using block ensures Dispose is called
            }
        }
    }
}

Output:

Static constructor called
Default constructor called
Disposing managed resources
Disposing unmanaged resources
Parameterized constructor: Resource2
Disposing managed resources
Disposing unmanaged resources

4. The this Keyword

Q: What is the this keyword in C#?

The this keyword refers to the current instance of a class. It is used to:

Q: Can you give an example of the this keyword in C#?

using System;

namespace ThisKeyword
{
    class Person
    {
        private string name;
        private int age;

        // Constructor using this to disambiguate
        public Person(string name, int age)
        {
            this.name = name; // this.name refers to the field
            this.age = age;
        }

        // Constructor chaining with this
        public Person(string name) : this(name, 18) // Default age
        {
        }

        // Method using this to pass current object
        public void PrintInfo(Person other)
        {
            Console.WriteLine($"Comparing {this.name} with {other.name}");
        }

        public void Introduce()
        {
            Console.WriteLine($"Hi, I'm {this.name}, {this.age} years old.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person("Krishna", 25);
            Person p2 = new Person("Kristal"); // Uses chained constructor

            p1.Introduce();
            p2.Introduce();

            p1.PrintInfo(p2); // Pass p2 to p1's method
        }
    }
}

Output:

Hi, I'm Krishna, 25 years old.
Hi, I'm Kristal, 18 years old.
Comparing Krishna with Kristal

Q: How do constructors and this differ from C/C++?

5. Common Mistakes & Best Practices

Q: Common mistakes?

Classes/Objects:

Constructors:

Destructors/IDisposable:

this:

Q: Best practices?

Classes/Objects:

Constructors:

Destructors/IDisposable:

this:

General: