Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learning to use Interfaces effectively

Tags:

c#

I've been developing software in C# for a while, but one major area that I don't use effectively enough is interfaces. In fact, I'm often confused on the various ways they can be used and when to use them. For example, I know methods can return interfaces, can take them as parameters, can be derived from them etc. This concept is a definite weakness for me

I was wondering if anyone knew of a source / tutorial that clearly and thoroughly explains interfaces in depth and the various ways they can be used?

like image 351
Hosea146 Avatar asked Jan 02 '12 13:01

Hosea146


People also ask

What is the purpose of using interfaces?

You use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are useful for the following: Capturing similarities among unrelated classes without artificially forcing a class relationship.

Should you always use interfaces?

No, every class should not have an interface. It's overkill squared. You use an interface when you need to abstract what's done from how it's done, and you're certain that the implementation can change.

Why should you program to an interface and why is this important?

If you program against abstractions, you can't couple yourself to a specific implementation. Interfaces allow you to make the coupling between your classes very loose. Classes should be developed and tested in isolation with few or no external dependencies.

Why should we favor programming to interfaces over implementations?

It allows the author to change how the class works, its implementation, without breaking how people interact with it. And you can program to the interface, not the implementation without ever using an Interface construct.


1 Answers

Description

Interfaces in C # provide a way to achieve runtime polymorphism. Using interfaces we can invoke functions from different classes through the same Interface reference, whereas using virtual functions we can invoke functions from different classes in the same inheritance hierarchy through the same reference.

For example:

public class FileLog : ILog {     public void Log(string text)     {         // write text to a file     } }  public class DatabaseLog : ILog {     public void Log(string text)     {         // write text to the database     } }  public interface ILog {     void Log(string text); }  public class SomeOtherClass {     private ILog _logger;      public SomeOtherClass(ILog logger)     {         // I don't know if logger is the FileLog or DatabaseLog         // but I don't need to know either as long as its implementing ILog         this._logger = logger;         logger.Log("Hello World!");     }     } 

You asked for tutorials.

Tutorials

  • MSDN - Interfaces (C# Programming Guide)
  • CodeGuru - Interfaces in C#
  • C# Interface Based Development
  • Codeproject - Interfaces in C# (For Beginners)
like image 133
dknaack Avatar answered Oct 14 '22 08:10

dknaack