0

In Java, assuming that class A is an interface/abstract class, I can do the following:

callMethod(new A(){

     public void myFunc(){
     System.out.println("test");
     }
});

How can I achieve the same shortcut effect in C# without having to declare a class seperately .

Thanks

Snake
  • 14,228
  • 27
  • 117
  • 250
  • 1
    In C# you cannot implement an interface with an anonymous class. But in C# there is much less reason to do so. There is no such thing as a `FunctionalInterface`. Instead we have delegates, and we use delegate objects rather than implementations of a `FunctionalInterface`. Instead, the signature of `CallMethod(Action action)` and you would pass in `() => Console.WriteLine("test")`. – Aron Jun 28 '16 at 01:52
  • 1
    Voting to reopen as this question actually looks like a question on `FunctionalInterface` in JAVA and `Delegate`s in C#. – Aron Jun 28 '16 at 01:53
  • Thank you. That looks interesting. I didn't even know this existed as I am learning c# – Snake Jun 28 '16 at 02:30

1 Answers1

0

You can't.

C# does not allow you to implement interfaces on Anonymous Types.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536