Declaring Properties in Interfaces in C#

Declaring Properties in Interfaces

In C#, interfaces can define properties without implementation, requiring implementing classes to provide definitions. This ensures a standardized contract across multiple classes.

Key Features of Properties in Interfaces

  • Interfaces can define read-only, write-only, or read-write properties.
  • Implementing classes must provide the property logic.
  • C# 8.0+ allows default implementations for properties in interfaces.
  • Encourages abstraction and code reusability.

Example: Declaring and Implementing Properties

The following example demonstrates how to declare and implement properties in interfaces.

Example: Interface with Properties

interface IPerson
{
    string Name { get; set; }  // Read-write property
    int Age { get; }           // Read-only property
}

// Implementing the interface
public class Student : IPerson
{
    public string Name { get; set; }
    public int Age { get; private set; }

    public Student(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

// Usage
Student student = new Student("Alice", 22);
Console.WriteLine($"Name: {student.Name}, Age: {student.Age}");

// student.Age = 25; // Error: Read-only property
        

In this example, IPerson defines a read-write property Name and a read-only property Age. The Student class implements these properties.

Read-Only and Write-Only Properties

Interfaces can declare properties with different accessibility levels.

Type Declaration Implementation Example
Read-Only int Age { get; } Value can be retrieved but not modified.
Write-Only string Password { set; } Value can be assigned but not read.
Read-Write string Name { get; set; } Value can be assigned and retrieved.

Default Implementations for Properties (C# 8+)

Starting from C# 8.0, interfaces can provide default property implementations.

Example: Default Property Implementation in Interface

interface IDevice
{
    string Model { get; set; }

    // Default property implementation (C# 8+)
    int Warranty { get; set; } = 1;  
}

// Implementing interface
class Laptop : IDevice
{
    public string Model { get; set; }
}

// Usage
Laptop myLaptop = new Laptop { Model = "Dell XPS" };
Console.WriteLine($"Model: {myLaptop.Model}, Warranty: {((IDevice)myLaptop).Warranty}");
        

In this example, IDevice provides a default implementation for the Warranty property, which can be accessed via an interface reference.

Auto-Implemented vs. Manually Implemented Properties

Properties in interfaces can be implemented using auto-implemented or manually implemented logic.

Implementation Type Example
Auto-Implemented public string Name { get; set; }
Manually Implemented
private string _name;
public string Name
{
    get { return _name; }
    set { _name = value; }
}
                    

When to Use Interface Properties?

  • When defining a common structure across multiple classes.
  • To enforce read-only or write-only property rules.
  • To provide default implementations for specific scenarios.
  • When creating loosely coupled architectures.