Instantiating Interfaces in C#?

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?

0

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

2

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>();

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.

2

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);
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật