This project is read-only.
1
Vote

Anonymous interfaces

description

New feature request: Anonymous interfaces

Possibility to declare intrface without naming it:
{ Name: string, Age: int, SayHello: string() } user = null;
or as a method parameter
public void SaveUser ({ Name: string, Age: int, SayHello: string() } user)
{
   //some code for saving user
}
Then we can call this method by passing instance of type that will fit this interface:
public class User
{
   public string Name; //Name: string
   public string Email; 
   public int Age; //Age: int
   public string SayHello() { Console.Writeline("Hello"); } //SayHello: string()
}

SaveUser(new User());
This won't work if type does not fit interface:
public class User
{
   public string Name; 
   public string Email;
   //public int Age; -> missing member
   public string SayHello() { Console.Writeline("Hello"); }
}

SaveUser(new OtherUser()); // exception "Missing Age member"
Also possibility to anonymously implement interface:
SaveUser(new
{
   Name = "Janusz",
   Age = 31,
   SayHello = () => "Hello from Janusz"
});

comments