0

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.

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • 3
    It is not possible in C#. – Guru Stron Jun 08 '21 at 13:44
  • There is an proposal to implement [anonymous interface objects](https://github.com/dotnet/csharplang/issues/4301) you can track. – Guru Stron Jun 08 '21 at 13:55
  • If `Foo` is abstract then you cannot `new Foo()` as it is not fully defined. The same what you cannot `new IFoo()` when `IFoo` is an interface. – JAlex Jun 08 '21 at 14:51
  • We can't do that in C# yet. Also: you don't "*instantiate an interface*", you **implement** it. And you can't "*instantiate abstract class*", it's a violation of OOP Theory. Please learn the minimal tech vocabulary before posting such an advanced question and employ terms anyhow. Otherwise C# is not Java nor Javascript, and even less Python or Ruby. But perhaps in the future we will be able to do that. I hope not: I don't see the utility, generics are for that and will be improved, for example to allow true generic polymorphism on open types with the diamond operator (covariance is a mess). –  Jun 08 '21 at 15:09
  • @OlivierRogier "In Java, I can create an instance of an anonymous inline implementation of an interface" – theonlygusti Jun 08 '21 at 20:07
  • @theonlygusti I prefer this sentence that sounds right, thanks. It was I understood. Like anonymous delegates and tuples. C# has not that (yet) for instances of classes and structs, as I know. –  Jun 08 '21 at 20:09
  • @OlivierRogier if `Pet` is a class that implements the interface `INameable`, and `dog` is an instance of `Pet`, is `dog` also an instance of `INameable`? What is the name of the relationship between `dog` and `INameable`? – theonlygusti Jun 08 '21 at 20:13
  • @theonlygusti From a scrupulous point of view: no. Because you can't create an instance of an interface. An interface is not an instance of an object. Therefore a variable can be type of an interface that points to or refers an object instance. Like a reference is not the object instance itself, but points towards it. [What is the difference between an interface and a class?](https://stackoverflow.com/questions/10914802/what-is-the-difference-between-an-interface-and-a-class-and-why-i-should-use-an/58174007#58174007) –  Jun 08 '21 at 20:22

1 Answers1

0

Such a feature is not available in c#. A workaround could be to use delegates:

public class DelegateFoo : Foo {
    private Func<Bar> barMethod();
    public DelegateFoo(Func<Bar> tmp) => barMethod = tmp;
    public override Bar Method() => barMethod();
}

var f = new DelegateFoo(() => new Bar());

But in such a simple case I the class does not add anything, so you might as well use a delegate directly.

JonasH
  • 28,608
  • 2
  • 10
  • 23