Sobes.tech
Junior — Middle

Could you provide an example of defining an interface in code?

sobes.tech AI

Answer from AI

In C#, an interface is defined using the keyword interface. It describes a set of methods and properties that a class must implement but does not contain implementations. Example:

public interface ILogger
{
    void Log(string message);
}

public class ConsoleLogger : ILogger
{
    public void Log(string message)
    {
        Console.WriteLine(message);
    }
}

In this example, ILogger is an interface with a Log method. The ConsoleLogger class implements this interface and provides a concrete implementation of the method.