Home > Net >  How override void method
How override void method

Time:10-08

I have abstract class with virtual method:

public virtual async Task Response(CancellationToken cancellationToken)
{
    some logic here..
}

In child class I don't need this logic is there more elegant way to do this:

public override async Task Response(CancellationToken cancellationToken)
{
    return;
}

CodePudding user response:

public override Task Response(CancellationToken cancellationToken)
{
    return Task.CompletedTask;
}

note no async modifier here. This avoids all allocations and state machinery, by simply reusing a single shared Task instance that is marked as pre-completed.

CodePudding user response:

public override async Task Response(CancellationToken cancellationToken) { base.Response(cancellationToken); }

if you use virtual word you have to override the parent method. otherwise you have to use the abstract method.

  •  Tags:  
  • c#
  • Related