Why can not we return a void call on a method that expects void return?

5

I was doing C # tests to find out how the void return issue works.

I did the following test below and realized that tests 1 and 3 work perfectly, but 2 does not.

See:

public class Program
{

    public void Test1()
    {
    }

    public void Test2()
    {

        return Test1();
    }

    public void Test3()
    {
        return;
    }

}

Error generated:

  

since 'Program.Test2 ()' returns void, a return keyword must not be followed by an object expression

My question is: Why is an error generated in the statement of Test2 , and does it actually return a void ?

The test was done at dotnetfiddle

    
asked by anonymous 06.12.2017 / 20:02

4 answers

5

There is something called Type System which is a set of rules that determine how the data will behave and what you can do with them. It is based on type theory .

So all language data structures are somehow based on these rules set by the type system of the language you are using.

Then the rules for void are special. Does it make sense to be like this? I think it does. It would make sense for him to be a normal guy. I would do it. Every decision has its implication.

A return can be used without anything or can be used with an expression. It can only be used if the method signature indicates that it should not return something (with void ). If the method is void it can not return something, and the syntax used is returning something.

Then you will say, "is not returning something, the method called is void and void indicates that there is something there", or say "is returning void to something expecting void . but this is circumstantial, it may change.But the worst is that it implies that something is being returned since by beating the eye% cos it seems that there is something there, only further investigation will indicate that it is even suitable. p>

Furthermore this is a matter of consistency, in several places you can not use it so, why would you use it in return algumacoisa ? What gain do you expect to have?

So it has been defined that any method that is return can only be called where a statement is expected and not where an expression is expected. This is more readable and works fine with no downside:

Test1();
return;

Want to in a row?

Test1(); return;

Any feature needs to be paid. There has to be a justification for its adoption, it has to give a real benefit and not charge anything significant.

Informally we even say "method returns void ", but this is conceptually wrong.

Other languages may be different.

You have proposal to allow this , but it was not very well accepted. Another .

Canonical Response .

    
06.12.2017 / 22:06
4

Because it makes no sense at all.

If the method is void , nothing will be returned , then it makes no sense to try to return something, even if it is the return of a function that is also void . >

If you need to call the Test1() function before finishing Test2 , just call the function.

public void Test1() { }

public void Test2() 
{
    Test1();
}
    
06.12.2017 / 20:07
2

According to the documentation link void (C # Reference) , when using void it specifies that the method will not return any value

void is also used in an unprotected context to declare a pointer to an unknown type , also note that void is a nickname for System.Void (en) > and as the link states that when used it will specify that the return type should not return value, so what I understand of this is that even though System.Void is:

  • ObjectValueTypeVoid

Even though the method where return Teste2(); has already been checked does not return anything and therefore no expression within return is accepted in the method, so when you add Teste2() inside return it will be an expression, regardless of the type of value that Test2() returns.

You could even do something like public in Teste2() (even if does not make sense, this is just to explain where "evaluation" occurs) that would get the same error:

A return keyword must not be followed by any expression when method returns void
  

Note: translation of

A return keyword must not be followed by any expression
when method returns void
     

would be something like:

Uma palavra-chave 'return' não deve ser seguida por qualquer
expressão quando o método retorna 'void'
    
09.12.2017 / 15:26
1

What happens is that the return statement ends the execution of the method in which it appears and returns the control to the calling method. It can also return an optional value. If the method is a void type, the return statement may be omitted and when using a void return method, return will cause nothing underneath it to execute and thus returning the control to the calling method.

In your case you can make a call to your Test1 method before the return

And you can not do return Teste1() since void when used as the return type for a method, it specifies that the method does not return a value.

    public void Test1()
    {
    }

    public void Test2()
    {
        Test1();
        return;
       //nada mais será executado
    }

    public void Test3()
    {
        return;
    }

We could basically say that this can not happen because it's simply like the C # was specified by the development team.

    
06.12.2017 / 20:10