micahhoover
micahhoover

Reputation: 2160

Using interfaces directly in C#

I recently read in "Professional C# 4 and .NET 4" that:

You can never instantiate an interface.

But periodically I see things like this:

IQuadrilateral myQuad;

What are the limitations in using interfaces directly (without having a class inherit from the interface)? How could I use such objects (if they can even be called objects)?

For example instead of using a Square class that derives from IQuadrilateral, to what extent could I get away with creating an interface like IQuadrilateral myQuad?

Since interfaces don't implement methods, I don't think I could use any methods with them. I thought interfaces didn't have fields to them (only properties), so I'm not sure how I could store data with them.

Upvotes: 0

Views: 94

Answers (3)

Thraka
Thraka

Reputation: 2174

Background Knowledge

Think of an interface as a contract. In a contract between two people, it defines what is capable, what is expected from the parties involved. In programming, it works the same way. The interface defines what to expect, what must exist for you to conform to that interface. Therefore, since it only defines what to expect, it itself, doesn't provide the implementation, the "code under the covers" so to speak, does.

A property behaves like a field, but allows you to intercept when someone assigns a value to it or reads the value. You can also deny reading or writing to it, your choice when you define the property. Interfaces work with properties instead of fields because of this. Since the "contract" is just defining what property should be there (name and type), and if it should allow a read or write capabilities, it leaves it up to the implementer to provide this.

Take for example the IEnumerator interface from the .NET framework. This interface was designed to allow iteration over a collection of objects. The purpose is not to change items, or randomly access them, it's just for getting object A and moving to the next, and the next, and the next, as many times as needed. Many collection type classes implement this: Queue, ArrayList, SortedList, Stack, etc. All these types of objects store many objects and now they all share the common "contract": the ability to iterate one-by-one over them.

However, you can see that the IEnumerator interface has a MoveNext() method declared. Why? This is because the items may not be served in the same manner. For example, people will generally access the ArrayList from the first item to the last. But a Stack was designed opposite, for people to access the last object down to the first.

Questions Answered

With all this knowledge, the limitation of declaring a variable as the interface type as opposed to the class type that implemented the interface is that you only get access to what the interface (the contract) says should be there. The benefit though is that you can assign to this variable any class type that implements the interface.

Upvotes: 0

psubsee2003
psubsee2003

Reputation: 8751

The answer is simple, you can't instantiate an interface.

The example you provided is not an example of instantiating an interface, you are just defining a local variable of the type IQuadrilateral

To instantiate the interface, you would have to do this:

IQuadrilateral myQuad = new IQuadrilateral();

And that isn't possible since IQuadrilateral does not have a constructor.

This is perfectly valid:

IQuadrilateral myQuad = new Square();

But you aren't initiating IQuadrilateral, you are initiating Square and assigning it to a variable with the type IQuadrilateral.

The methods available in myQuad would be the methods defined in the interface, but the implementation would be based on the implementation in Square. And any additional methods in Square that are not part of the IQuadrilateral interface would not be available unless you cast myQuad to a Square variable.

Upvotes: 4

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174467

You can't create an instance of an interface.

The code you showed defines a variable of type IQuadrilateral. The actual instance this variable points to will always be of a concrete class implementing this interface.

Upvotes: 2

Related Questions