Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
// Defining and implementing interfaces public interface IShape { double Area { get; } double Perimeter { get; } string Describe() => $"{GetType().Name}: area={Area:F2}, perimeter={Perimeter:F2}"; } public interface IResizable { void Scale(double factor); } // Implementing multiple interfaces public class Circle : IShape, IResizable { public double Radius { get; private set; } public Circle(double radius) => Radius = radius; public double Area => Math.PI * Radius * Radius; public double Perimeter => 2 * Math.PI * Radius; public void Scale(double factor) => Radius *= factor; } // Using the interface type (abstraction) static void PrintShape(IShape shape) => Console.WriteLine(shape.Describe()); var c = new Circle(5); PrintShape(c); // uses default interface method ((IResizable)c).Scale(2); PrintShape(c); // radius is now 10 // IDisposable pattern — ensures cleanup of unmanaged resources public class FileLogger : IDisposable { private StreamWriter? _writer; public FileLogger(string path) => _writer = new StreamWriter(path, append: true); public void Log(string message) => _writer?.WriteLine($"{DateTime.UtcNow:O} {message}"); public void Dispose() { _writer?.Flush(); _writer?.Dispose(); _writer = null; } } // 'using' statement calls Dispose automatically using var logger = new FileLogger("app.log"); logger.Log("Application started");
Result
Open