Does C# have an equivalent to Scala's structural typing?

Posted by Tom Morris on Stack Overflow See other posts from Stack Overflow or by Tom Morris
Published on 2010-05-14T00:42:05Z Indexed on 2010/05/14 0:44 UTC
Read the original article Hit count: 380

In Scala, I can define structural types as follows:

type Pressable { def press(): Unit }

This means that I can define a function or method which takes as an argument something that is Pressable, like this:

def foo(i: Pressable) { // etc.

The object which I pass to this function must have defined for it a method called press() that matches the type signature defined in the type - takes no arguments, returns Unit (Scala's version of void).

I can even use the structural type inline:

def foo(i: { def press(): Unit }) { // etc.

It basically allows the programmer to have all the benefits of duck typing while still having the benefit of compile-time type checking.

Does C# have something similar? I've Googled but can't find anything, but I'm not familiar with C# in any depth. If there aren't, are there any plans to add this?

© Stack Overflow or respective owner

Related posts about scala

Related posts about c#