In Java I can instantiate an on-the-spot implementation of an interface:
interface Rubbable {
public void rub();
}
...
Rubbable r = new Rubbable() {
@Override
public void rub() {
// implementation
}
};
Like, a one-time implementation kind of thing.
I've tried to do the same thing with an abstract class in C#
public abstract class Foo
{
public abstract Bar Method();
}
Foo f = new Foo
{
public override Bar Method()
{
return new Bar();
}
}
But I'm getting the error
Cannot create an instance of the abstract type or interface 'Foo'
Can I instantiate an abstract class on-the-spot as I would like to? Some kind of, one-time-implementation without having to dedicate a whole class declaration in another part of the codebase to it.