Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am reading/learning about interfaces in C# at the moment, and thus far I managed to understand how it differs from an abstract class. In the book I am reading the author explains that interfaces are the ultimate abstract class and that it simply sets the standard of certain methods the inheriting class will have, but then provides the following example.

static void Main(string[] args)
{
...
Circle c = new Circle("Lisa");
IPointy itPt = null;
try
{
itPt = (IPointy)c;
Console.WriteLine.(itPt.Points);
}
catch (InvalidCastException e)
{
 Console.WriteLine(e.Message);
}
...
}

The line that absolutely throws me off is the IPointy itfPt=null; did he just declare an interface? I thought interfaces are abstract and can only be inherited? What kind of sorcery is going on here?

share|improve this question
add comment

4 Answers

up vote 9 down vote accepted

That example demonstrates a try...catch more than anything.

Interfaces are types too.. but they only expose their contract. So this:

IPointy itPt = new IPointy();

..won't work. Consider this:

interface IPointy {
    void MyMethod();
}

class Pencil : IPointy {
    void MyMethod() {
    }

    void MyOtherMethod() {
    }
}

You can declare an IPointy like this:

IPointy itPt = new Pencil();

..however, this will work:

itPt.MyMethod();

..this won't work:

itPt.MyOtherMethod();

This is because MyMethod is part of the IPointy contract.. MyOtherMethod isn't.. Hopefully that helps.

EDIT: Expanding this to explain my comment.

Inheriting from a class represents an "is-a" relationship. Implementing an interface represents a "can-do" relationship.

Consider:

interface ISwitchable {
    void SwitchOn();
    void SwitchOff();
}

class Light : ISwitchable {
    void SwitchOn() { }
    void SwitchOff() { }
}

class Television : ISwitchable {
    void SwitchOn() { }
    void SwitchOff() { }
}

Both a Television and Light inherit from ISwitchable.. which is fine. The interface defines a "can-do" relationship.. they can both be switched on orr of. Both of the following blocks of code are valid:

ISwitchable mySwitchableObject1 = new Light();
ISwitchable mySwitchableObject2 = new Television();

mySwitchableObject1.SwitchOn(); // calls Light's SwitchOn method
mySwitchableObject2.SwitchOn(); // calls Television's SwitchOn method
share|improve this answer
1  
Ah that is interesting, interface acts almost like a filter, neat stuff! –  RealityDysfunction Oct 7 '12 at 0:18
    
Updated my answer to show you a little bit more. –  Simon Whitehead Oct 7 '12 at 0:54
add comment

You can declare variables typed as interfaces: that does not amount to instantiating them. Once you declare a variable of interface type, you can assign it an object of any class that implements the interface.

For example, you can declare a variable of interface type IDictionary<string,string>, but you cannot instantiate it: you must choose a class that implements IDictionary<string,string>, for example

IDictionary<string,string> d = new Dictionary<string,string>();

or

IDictionary<string,string> d = new SortedList<string,string>();
share|improve this answer
add comment

I think what you look for is instantiation of Interface and use of it. Here is an example that may help you:

public interface IFoo
{
    void Bar1();
    void Bar2();
}

public Class ActionableFoo : IFoo
{
    Action _bar1, _bar2;

    public ActionableFoo(Action b1, Action b2)
    {
        _bar1 = b1;
        _bar2 = b2;
    }

    public void Bar1() { if(_bar1 != null) _bar1(); }
    public void Bar2() { if(_bar2 != null) _bar2(); }
}

public void PushFoo(Action bar1, Action bar2)
{
    IFoo foo = new ActionableFoo(bar1, bar2);
    _fooStack.Push(foo);
}
share|improve this answer
add comment

An interface is declared using the interface keyword, which that line of code does not contain.

What you're seeing is a variable declaration, a variable named itfPt, whose type is IPointy. The variable gets the value 'null', which is not an instance.

Instantiating the interface IPointy is not possible, you can try doing it by type itfPt = new IPointy(); and examining the compile errors. The only values that can be assigned to itfPt are instances of concrete subtypes of IPointy and null.

share|improve this answer
    
Hmm, I see. So what would itPt be after he declares it as a variable? It is not a class or a data storing variable, right? –  RealityDysfunction Oct 7 '12 at 0:03
    
After he declares it as a variable it will be a variable. A variable is not a class. A variable does store data, yes. –  Ricky Clarkson Oct 7 '12 at 3:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.