-4

Hi there I've been doing a lot of research on interfaces in C# and I've read so many books and watched videos but eh the light bulb just hasn't turned on for me just yet :-(

​Q1 Lets say we have a method and its name is Show() in the class where the implementation happens, and inside that method we have an array and it implements the interface ISample with a Show() method.

Now If the internals change of that method in the implementation from an array to List

Because we're using an interface does this mean it won't break If it's being called in thousands of places as it's still calling the method name and doesn't care what the internals do once the contract is kept between the method name and the name of the interface method keeping the contract promise ?

Hopefully I've clarified that better so somebody can clear this up for me. Thanks in advance for the replies :)

  • Not sure if this question is appropriate for SO. There are many resources that can tell you what a C# Interface is and you should play with it yourself. https://msdn.microsoft.com/en-us/library/87d83y5b.aspx https://msdn.microsoft.com/en-us/library/ms173156.aspx – Kody Jan 09 '17 at 22:58
  • 3
    Is there a specific question you have? – Broots Waymb Jan 09 '17 at 22:58
  • 1
    You might find more help on http://softwareengineering.stackexchange.com/ – Klitos Kyriacou Jan 09 '17 at 22:59
  • Yes and I've tried to understand those before coming here, I don't just post bride doing my research, I've been really trying to understand this and I thought well I'll ask on stack overflow and maybe somebody can help me :-) – Projectrevolution Jan 09 '17 at 23:01
  • "But if you program to an implementation then once you change the method it has to be changed everywhere else that invokes that method." Yes because you broke the contract. The point is if you have a public interface, be very cautious about changing it especially if all the code that has been written against it, is not your own. If it is your own code you can change it but even that requires work. Still not sure what your question is. Please edit your question and state which parts you need clarity on. – CodingYoshi Jan 09 '17 at 23:08
  • @KlitosKyriacou when referring other sites, it is often helpful to point that [cross-posting is frowned upon](http://meta.stackexchange.com/tags/cross-posting/info) – gnat Jan 09 '17 at 23:12
  • 1
    @Projectrevolution Furthermore, interfaces are not to compensate for C#'s limitation of single inheritance. If I have a class which implements `IComparable` it does not mean I am inheriting `IComparable`. – CodingYoshi Jan 09 '17 at 23:12

3 Answers3

0

You hit the nail on the head with one of your own examples

If the internals change of that method in the implementation from an array to List

Suppose you are writing a reporting class and you want to count the number of items from a variety of sources returning the types List, List, Collection, Stack, and Queue. You could write one method for each returned type, and continue writing new counters for each new type you come across. Or you can write a single counter method that accepts any IEnumerable.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
0

Interfaces can improve maintainability (if done correctly). Interfaces can also be used to give the effect of multiple inheritance but they are distinct concepts.

The key to understanding interfaces is to understand the concept of coupling. Coupling essentially means how intertwined your code is. The more coupling, the more things will break when you make a change. When you think of tight coupling, think of a tight ball of tangled twine. What we want is a loose ball of twine with the fewest entanglements possible. Entanglements are bad because every time we change something, every entanglement to that change causes our code to break. Interfaces are one way to achieve loose coupling.

Here is a good thread that further explains coupling: What is "loose coupling?" Please provide examples

Community
  • 1
  • 1
Nathan
  • 1
  • 1
0

Interfaces have nothing to do with internals or implementation. They have to do with the ability to be called a certain way.

An interface is a promise made by a class to support a call with a specific signature. For example, if you have a list of controls that can be displayed, you might have an interface like

interface IShowable
{
     void Show();
}

If you have a dozen controls of various types (and classes) in a list, you can still show them with type-agnostic code, as long as they support the interface. For example:

var list = new List<IShowable>();
list.Add(new MyShowableTextBoxControl());    
list.Add(new MyShowableClockControl());    
list.Add(new MyShowableSmileyControl());
foreach (var item in list) list.Show();  //Magic!!

You can use this ability to your advantage when you want to write code that calls a library but you're not sure which library. Imagine, for example, you have not decided whether to use Oracle or SQL Server. If you have two classes

 class OracleLibrary : IGenericDatabaseLibrary

and

 class SqlServerLibrary : IGenericDatabaseLibrary

...then in the main part of your code, you can safely call anything in the generic database library without worrying about whether you are calling Oracle or SQL Server.

IGenericDatabaseLibrary db = Factory.GetDatabaseLibrary(); //We don't know what this is going to return
db.ExecuteQuery(sql); //As long as ExecuteQuery is in the interface, we know we can call it
John Wu
  • 50,556
  • 8
  • 44
  • 80