Anonymous class implementing interface

Posted by Flo on Stack Overflow See other posts from Stack Overflow or by Flo
Published on 2010-06-14T13:35:00Z Indexed on 2010/06/14 13:42 UTC
Read the original article Hit count: 286

Filed under:
|
|

I have the following code inside a method:

 var list = new[]
  {
   new { Name = "Red", IsSelected = true },
   new { Name = "Green", IsSelected = false },
   new { Name = "Blue", IsSelected = false },
  };

I would like to call a function that requires a list of elements with each element implementing an interface (ISelectable). I know how this is done with normal classes, but in this case I am only trying to fill in some demo data.

Is it possible to create an anonymous class implementing an interface?

like this:

new { Name = "Red", IsSelected = true } : ISelectable

© Stack Overflow or respective owner

Related posts about c#

Related posts about interface