Junior — Middle
What is the role of interfaces in software development?
sobes.tech AI
Answer from AI
Interfaces in software development serve to define a contract — a set of methods and properties that a class must implement, without specifying the concrete implementation. This allows:
- Ensuring loose coupling between components.
- Simplifying the replacement and extension of functionality without changing the code that uses the interface.
- Supporting polymorphism, enabling working with different implementations through a common interface.
In C#, an interface is declared using the interface keyword and can contain only method signatures, properties, events, and indexers.
Example:
public interface ILogger
{
void Log(string message);
}
public class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
Thus, interfaces help build flexible and extensible architectures.