In C#, interfaces define a contract that classes can implement, specifying a set of methods, properties, and events that the implementing classes must provide. Here’s a list of 50 common interfaces in C# along with brief explanations and common methods for each, along with code snippet examples for some of them:

  1. IEnumerable:
  • Method: GetEnumerator()
  • Explanation: Provides a way to iterate through a collection.
   IEnumerable<int> numbers = new List<int> { 1, 2, 3 };
   foreach (var number in numbers) { /* ... */ }
  1. IComparable:
  • Method: CompareTo(object obj)
  • Explanation: Allows objects to be compared to determine their ordering.
   public class MyClass : IComparable<MyClass> { /* ... */ }
  1. IEquatable:
  • Method: Equals(T other)
  • Explanation: Ensures objects can be compared for equality.
   public class MyClass : IEquatable<MyClass> { /* ... */ }
  1. IDisposable:
  • Method: Dispose()
  • Explanation: Provides a way to release unmanaged resources.
   public class MyDisposable : IDisposable { /* ... */ }
  1. IList:
  • Methods: Add, Remove, IndexOf, Insert, Count, Clear, Contains, CopyTo
  • Explanation: Represents a non-generic collection of objects.
   IList<int> numbers = new List<int>();
   numbers.Add(42);
  1. IDictionary:
  • Methods: Add, Remove, TryGetValue, ContainsKey, ContainsValue, Keys, Values
  • Explanation: Represents a collection of key-value pairs.
   IDictionary<string, int> dictionary = new Dictionary<string, int>();
   dictionary.Add("One", 1);
  1. INotifyPropertyChanged:
  • Method: PropertyChanged
  • Explanation: Notifies clients that a property value has changed.
   public class MyViewModel : INotifyPropertyChanged { /* ... */ }
  1. IEnumerable:
  • Method: GetEnumerator()
  • Explanation: Represents a strongly-typed collection of objects.
   IEnumerable<int> numbers = new List<int> { 1, 2, 3 };
   foreach (var number in numbers) { /* ... */ }
  1. IQueryable:
  • Methods: Various LINQ methods
  • Explanation: Enables LINQ query operations on data sources.
   IQueryable<string> query = dataSource.Where(item => item.Length > 5);
  1. IComparable:
    • Method: CompareTo(T other)
    • Explanation: Allows strongly-typed comparison of objects.
    public class MyClass : IComparable<MyClass> { /* ... */ }
  2. IEquatable:
    • Method: Equals(T other)
    • Explanation: Ensures strongly-typed equality comparison.
    public class MyClass : IEquatable<MyClass> { /* ... */ }
  3. IList:
    • Methods: Add, Remove, IndexOf, Insert, Count, Clear, Contains, CopyTo
    • Explanation: Represents a generic list of objects.
    IList<int> numbers = new List<int>(); numbers.Add(42);
  4. IDictionary:
    • Methods: Add, Remove, TryGetValue, ContainsKey, ContainsValue, Keys, Values
    • Explanation: Represents a generic collection of key-value pairs.
    IDictionary<string, int> dictionary = new Dictionary<string, int>(); dictionary.Add("One", 1);
  5. IFormattable:
    • Method: ToString(string format, IFormatProvider formatProvider)
    • Explanation: Provides a mechanism for formatting objects as strings.
    public class MyData : IFormattable { /* ... */ }
  6. ICloneable:
    • Method: Clone()
    • Explanation: Allows an object to create a shallow copy of itself.
    public class MyCloneable : ICloneable { /* ... */ }
  7. IConvertible:
    • Methods: Various conversion methods
    • Explanation: Provides methods to convert an object to a different data type.
    int value = Convert.ToInt32("42");
  8. IAsyncResult:
    • Methods: AsyncWaitHandle, CompletedSynchronously, IsCompleted
    • Explanation: Represents the status of an asynchronous operation.
    IAsyncResult asyncResult = SomeAsyncMethod.BeginInvoke(callback, state);
  9. ICustomFormatter:
    • Method: Format(string format, object arg, IFormatProvider formatProvider)
    • Explanation: Custom string formatting.
    public class MyFormatter : ICustomFormatter { /* ... */ }
  10. IEqualityComparer:
    • Methods: Equals, GetHashCode
    • Explanation: Compares objects for equality.
    IEqualityComparer<string> comparer = StringComparer.OrdinalIgnoreCase;
  11. IComparer:
    • Method: Compare
    • Explanation: Compares two objects for ordering.
    IComparer<int> comparer = Comparer<int>.Default;
  12. IAsyncResult:
    • Methods: AsyncWaitHandle, CompletedSynchronously, IsCompleted
    • Explanation: Represents the status of an asynchronous operation.
    IAsyncResult asyncResult = SomeAsyncMethod.BeginInvoke(callback, state);
  13. IConvertible:
    • Methods: Various conversion methods
    • Explanation: Provides methods to convert an object to a different data type.
    int value = Convert.ToInt32("42");
  14. ISerializable:
    • Methods: GetObjectData
    • Explanation: Provides control over object serialization.
    [Serializable] public class MySerializable : ISerializable { /* ... */ }
  15. IComparable:
    • Method: CompareTo(T other)
    • Explanation: Allows strongly-typed comparison of objects.
    public class MyClass : IComparable<MyClass> { /* ... */ }
  16. IList:
    • Methods: Add, Remove, IndexOf, Insert,
    Count, Clear, Contains, CopyTo
    • Explanation: Represents a generic list of objects.
    IList<int> numbers = new List<int>(); numbers.Add(42);
  17. IDictionary:
    • Methods: Add, Remove, TryGetValue, ContainsKey, ContainsValue, Keys, Values
    • Explanation: Represents a generic collection of key-value pairs.
    IDictionary<string, int> dictionary = new Dictionary<string, int>(); dictionary.Add("One", 1);
  18. IFormattable:
    • Method: ToString(string format, IFormatProvider formatProvider)
    • Explanation: Provides a mechanism for formatting objects as strings.
    public class MyData : IFormattable { /* ... */ }
  19. IComparable:
    • Method: CompareTo(object obj)
    • Explanation: Allows objects to be compared to determine their ordering.
    public class MyClass : IComparable<MyClass> { /* ... */ }
  20. IEquatable:
    • Method: Equals(T other)
    • Explanation: Ensures objects can be compared for equality.
    public class MyClass : IEquatable<MyClass> { /* ... */ }
  21. IDisposable:
    • Method: Dispose()
    • Explanation: Provides a way to release unmanaged resources.
    public class MyDisposable : IDisposable { /* ... */ }
  22. ISerializable:
    • Methods: GetObjectData
    • Explanation: Provides control over object serialization.
    [Serializable] public class MySerializable : ISerializable { /* ... */ }
  23. IComparer:
    • Method: Compare
    • Explanation: Compares two objects for ordering.
    IComparer<int> comparer = Comparer<int>.Default;
  24. IEqualityComparer:
    • Methods: Equals, GetHashCode
    • Explanation: Compares objects for equality.
    IEqualityComparer<string> comparer = StringComparer.OrdinalIgnoreCase;
  25. IAsyncDisposable (C# 8.0 and later):
    • Method: DisposeAsync()
    • Explanation: Provides asynchronous disposal of resources.
    public class MyAsyncDisposable : IAsyncDisposable { /* ... */ }
  26. INotifyPropertyChanged:
    • Method: PropertyChanged
    • Explanation: Notifies clients that a property value has changed.
    public class MyViewModel : INotifyPropertyChanged { /* ... */ }
  27. IProgress:
    • Method: Report(T value)
    • Explanation: Reports progress updates during an asynchronous operation.
    async Task SomeAsyncOperation(IProgress<int> progress) { // Report progress: progress.Report(50); }
  28. IHttpHandler:
    • Method: ProcessRequest(HttpContext context)
    • Explanation: Represents an HTTP handler.
    public class MyHandler : IHttpHandler { /* ... */ }
  29. IHttpModule:
    • Methods: Init(HttpApplication context), Dispose()
    • Explanation: Represents an HTTP module.
    public class MyModule : IHttpModule { /* ... */ }
  30. IWebHostBuilder (ASP.NET Core):
    • Methods: Various methods for configuring a web host.
    • Explanation: Provides a builder pattern for configuring and building an ASP.NET Core web host.
    public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>();
  31. IConfiguration (ASP.NET Core):
    • Methods: Various methods for accessing configuration values.
    • Explanation: Provides access to application configuration settings.
    IConfiguration configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build();
  32. IApplicationBuilder (ASP.NET Core):
    • Methods: Various methods for configuring the application’s request pipeline.
    • Explanation: Used to configure the middleware pipeline in ASP.NET Core applications.
    public void Configure(IApplicationBuilder app) { app.UseMiddleware<CustomMiddleware>(); }
  33. IServiceCollection (ASP.NET Core):
    • Methods: AddTransient, AddScoped, AddSingleton, and more for configuring services.
    • Explanation: Used for registering and configuring services in ASP.NET Core applications.
    public void ConfigureServices(IServiceCollection services) { services.AddTransient<IMyService, MyService>(); }
  34. IRouteBuilder (ASP.NET Core):
    • Methods: Various methods for defining routes.
    • Explanation: Used to configure routing in ASP.NET Core applications.
    public void Configure(IRouteBuilder routes) { routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); }
  35. IActionResult (ASP.NET Core):
    • Method: ExecuteResultAsync(ActionContext context)
    • Explanation: Represents the result of an MVC action method.
    public class MyController : Controller { public IActionResult Index() { return View(); } }
  36. ILogger (ASP.NET Core):
    • Methods: Various logging methods (LogInformation, LogError, etc.).
    • Explanation: Used for logging in ASP.NET Core applications.
    public class MyService { private readonly ILogger<MyService> _logger;public MyService(ILogger&lt;MyService&gt; logger) { _logger = logger; } public void DoWork() { _logger.LogInformation("Doing some work..."); }}
  37. IHostingEnvironment (ASP.NET Core):
    • Methods: IsDevelopment, IsStaging, IsProduction, etc.
    • Explanation: Provides information about the hosting environment in ASP.NET Core applications.
    public class MyMiddleware { public MyMiddleware(IHostingEnvironment env) { if (env.IsDevelopment()) { // Do something in the development environment. } } }
  38. IUrlHelper (ASP.NET Core):
    • Methods: Various methods for generating URLs.
    • Explanation: Used for generating URLs in ASP.NET Core applications.
      csharp public IActionResult MyAction() { var url = Url.Action("Index", "Home"); // ... }

48.

IQueryable:
Methods: Various LINQ methods
Explanation: Enables LINQ query operations on data sources.
csharp IQueryable<string> query = dataSource.Where(item => item.Length > 5);

  1. IAsyncEnumerable (C# 8.0 and later):
    • Methods: GetAsyncEnumerator()Explanation: Represents an asynchronous sequence of values.
    IAsyncEnumerable<int> asyncSequence = GetAsyncData(); await foreach (var item in asyncSequence) { /* ... */ }
  2. IAsyncEnumerator (C# 8.0 and later):
    • Methods: MoveNextAsync(), Current
    • Explanation: Provides asynchronous iteration over a sequence of values.
      csharp IAsyncEnumerator<int> asyncEnumerator = GetAsyncData().GetAsyncEnumerator(); while (await asyncEnumerator.MoveNextAsync()) { var item = asyncEnumerator.Current; // ... }

By davs