Junior — Middle
How did you create a universal interface for different components or modules of the system?
sobes.tech AI
Answer from AI
To create a universal interface for different components or modules of a system in C#, I use interfaces. An interface defines a set of methods and properties that all classes supporting this interface must implement. This allows working with different implementations through a common type, ensuring flexibility and extensibility.
Example:
public interface ILogger
{
void Log(string message);
}
public class FileLogger : ILogger
{
public void Log(string message)
{
// Write message to a file
}
}
public class DatabaseLogger : ILogger
{
public void Log(string message)
{
// Write message to a database
}
}
// Usage
void Process(ILogger logger)
{
logger.Log("Start of process");
}
This approach makes it easy to add new implementations without changing the code that uses the interface.