Sobes.tech
Junior — Middle

What reasons allow assigning properties to interfaces in programming languages?

sobes.tech AI

Answer from AI

In C# starting from version 8.0, interfaces can have default property implementations. This allows:

  • Adding new members to interfaces without breaking existing implementations (backward compatibility).
  • Making interfaces more flexible and extensible.

For example, you can define a property with an implementation directly in the interface:

public interface ILogger
{
    string LogLevel { get; set; }  // property with default implementation
    void Log(string message);
}

public class ConsoleLogger : ILogger
{
    public string LogLevel { get; set; } = "Info";
    public void Log(string message)
    {
        Console.WriteLine($"[{LogLevel}] {message}");
    }
}

Thus, properties in interfaces increase expressiveness and ease of use.

What reasons allow assigning properties to interfaces… - sobes.tech