Casting as Interface? Awesome.

Posted by cam on Stack Overflow See other posts from Stack Overflow or by cam
Published on 2010-04-20T13:58:44Z Indexed on 2010/04/20 14:03 UTC
Read the original article Hit count: 204

Filed under:
|

I was just experimenting around with one of my programs, trying to make it looks prettier and I found something interesting. I have an interface that 4 classes inherit from.

I found that if I pass the Class as an object to a method like so:

ClassTest classtest = new ClassTest();

DoOperation(classtest);

private void DoOperation(object o)
{
((InterfaceClass)o).DoThis();
}

So I can pass any type of class that inherits from InterfaceClass, and it will preform the proper interface operation? This is the coolest thing I've ever found from OOP (something I've never really studied)

I really thought interfaces were created for the sole purpose of organization for developers. Are there more uses for interfaces than this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about interface