C# feature request: implement interfaces on anonymous types

前端 未结 10 1778
走了就别回头了
走了就别回头了 2021-02-19 06:57

I am wondering what it would take to make something like this work:

using System;

class Program
{
    static void Main()
    {
        var f = new IFoo { 
              


        
10条回答
  •  春和景丽
    2021-02-19 07:07

    You could have something like anonymous classes in Java:

    using System; 
    
    class Program { 
      static void Main() { 
        var f = new IFoo() {  
          public String Foo { get { return "foo"; } } 
          public void Print() { Console.WriteLine(Foo); }
        }; 
      } 
    } 
    
    interface IFoo { 
      String Foo { get; set; } 
      void Print(); 
    } 
    

提交回复
热议问题