-3

I was reading this http://msdn.microsoft.com/en-us/library/87d83y5b(v=vs.80).aspx but I am wondering, what is the difference between or benefit of using interfaces as opposed to simply creating a class with properties and adding it to your class via "using MyClass.cs?" It seems like either way you have to instantiate the method or property class...

Thanks for your advice.

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • 3
    This is a large topic, which has been answered before e.g. http://stackoverflow.com/questions/240152/why-would-i-want-to-use-interfaces http://stackoverflow.com/questions/1686174/when-should-one-use-interfaces Using interfaces can improve code design, reduce complexity, coupling and other benefits. – Jason Evans Feb 21 '13 at 17:56
  • Thank you, Jason, but I am not simply asking the benefits of using Interfaces, but rather, the differences between using Interfaces as opposed to using `using`. – user1477388 Feb 21 '13 at 17:57
  • Can you clarify what you mean by `using`? Do you mean the keyword `using` as in using (var foo = new Object()) or when importing namespaces? – Jason Evans Feb 21 '13 at 17:59
  • No, I mean at the top of a class where you might have `using System.IO` you could have `using MyClass.cs` to instantiate objects in `MyClass.cs`. – user1477388 Feb 21 '13 at 18:00
  • @user1477388 - I think you mean `using MyClass`, not `MyClass.cs`. The using directive is for assemblies, not individual files (as far as I know). – Tim Feb 21 '13 at 18:01
  • Could you post some source code in your question that would help clarify what you're asking? I think there is some doubt as to what you're asking is actually valid in C#. – Jason Evans Feb 21 '13 at 18:02
  • I guess I am talking about this http://stackoverflow.com/a/1423837/1477388 but I thought there were a more explicit implementation such as `using MyClass.cs`. I could be mistaken. – user1477388 Feb 21 '13 at 18:07

4 Answers4

2

Several different classes may all implement the same interface and thus share a specific set of characteristics. E.g. all types that implement the IEnumerable interface may be enumerated without having anything else in common. Interfaces allows less restrictive way for types to support specific features.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
  • I am still not sure I understand, because couldn't different classes share a common `using MyClass.cs`, too? – user1477388 Feb 21 '13 at 17:56
  • 1
    @user1477388 - sure, they could. But all the interface does is tell the implementing classes what methods they must implement. How the method is implemented is left up to the implementing class. – Tim Feb 21 '13 at 17:59
2

An interface is not so you can use the interface's objects. It's, rather, so that you must create those objects.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Thank you, ispiro, I see that is what Brian is trying to say in his comment, "implementing classes what methods they must implement." So, that is the big difference. – user1477388 Feb 21 '13 at 18:02
1

I don't see how the two are even similar. An interface defines how your code is used by clients. The using keyword (which should be followed by a namespace name rather than a file name) simply lets you use objects in that namespace without prefixing them with the entire namespace each time.

A more common question is what is the difference between implementing an interface and deriving from a class. Maybe that is what you were trying to ask. That question has been covered pretty extensively elsewhere.

djs
  • 220
  • 1
  • 5
1

Interface only contains the signature of your logic. It must be implemented fully in your child class. We use "using" clause when we want to include namespace which is different then the namespace of your project, but if your class or interface is in the same namespace you don't need to use "using" clause.

You can inherit your child class with interfaces and make your code more flexible.

An example of it would be:

public interface IClown
{
  string FunnyThingIHave { get; }
  void Honk();
}

public class TallGuy : IClown
{
  public string FunnyThingIHave {
  get { return "big shoes"; }
}
  public void Honk() {
    MessageBox.Show("Honk honk!");
  }
}
public class Joker:IClown
{
  public string FunnyThingIHave
  {
    get {return "I have a clown car"}
  }
  public void Honk()
  {
    MessageBox.Show("Honk Bonk");
  }
}

public class FunnyClowns 
{
  Joker joker = new Joker();
  TallGuy tguy = new TallGuy();

  string WhichFunnyThingIWant(IClown clownType)
  {
     clownType.Honk();
  }
}

Now what this is doing is defining a clown interface and then defining two child classes for it then a third class can dynamically call the clowntype object of IClown. This is a simple example but this kind of logic can be applied in many other situations. That is where interfaces can be really helpful. I hope this helps..

Faaiz Khan
  • 332
  • 1
  • 4