Expressed method: do not return anything

I upgraded one of our projects to C # 6.0 when I found a method that literally did nothing:

private void SomeMethod()
{
    return;
}

Now I was wondering if it is possible to rewrite it as a method with an expression, since it contains only return.

Try 1:

private void SomeMethod() => return;

Exceptions:

  • ; Expected,

  • Invalid 'return' token in class member declaration, structure or interface

  • Invalid expression term 'return'

Try 2:

private void SomeMethod() => ;

An exception:

  • Invalid expression term ';'

Is there any way to achieve this (although this method does not make sense)?

+8
source share
3 answers

This is not the body of an expression, but you can do this:

private void SomeMethod() { }
+7
source

, , - .

return:

  private void SomeMethod() { }

:

  private Action SomeMethod = () => { };
+2

If you really want to make an expression for body in the void function, you can do this:

 private void SomeFunction() => Expression.Empty();

This uses Linq and creates an empty expression of type Void.

0
source

All Articles