Why can’t we return a void call in a void return waiting method?

Asked

Viewed 814 times

8

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

I did the following test below and realized that tests 1 and 3 work perfectly, however 2 no.

Behold:

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 Test2, being that he actually returns a void?

The test was done on dotnetfiddle

4 answers

10


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

So all language data structures are somehow based on these rules established by the language type system that you’re using.

So the rules for the void are special. Does it make sense to be like this? I think it does. Would it make sense that he was a normal guy? He would do it. Every decision has its implication.

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

Then you’ll say:

is not returning something, the method called is void and void indicates that there is nothing there

or say:

is returning void for something that awaits void

Yes, but this is circumstantial, it can change. But the worst is that it suggests that something is being returned since hitting the eye on return algumacoisa it seems that there is something there, only a deeper investigation will indicate that it is even appropriate.

Also it is a matter of consistency, in several places can not use so, because it would let use in the return? What do I get?

So it was defined that whatever method void can only be called where one expects statement and not where one expects an expression. This is more readable and works well without any negative points:

Test1();
return;

Want to put in a line?

Test1(); return;

I put in the Github for future reference.

Whichever Feature need to pay. There has to be a justification for your adoption, have to give a real benefit and not charge anything significant.

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

Other languages may adopt different posture.

Has proposal to allow this, but it was not very well accepted. Other.

Canonical answer.

  • It is interesting to say that in the language Kotlin, there is a type corresponding to void, which is the Unit, and he can do exactly what the author asked. :)

  • The question of "one line" was exactly what I was looking for. Interesting your answer +1

7

'Cause it doesn’t make any sense.

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 function Test1() before you finish Test2, just call the function.

public void Test1() { }

public void Test2() 
{
    Test1();
}
  • But, if the Test2 return the int and Test1 also, it would work. In case void wouldn’t be "a guy"?

  • 3

    @Wallacemaxters No, void is nothing.

  • @LINQ I think void is something when using pointers, actually it can be anything, if the pointers of C# is like the language C.

  • 2

    @cat There is a different beeeeeeeeeeeeeeeeeeeeeem issue. First the compiler does not even let pointer exist without being inside a block unsafe, which means it’s a very special case

2

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

The void is also used in a unprotected context to declare a pointer to a unknown type, also note that void is a nickname for System.Void and as the link states that when used it will specify that the return type should not return value, so what I understand from this is that even if System.Void be it:

  • ObjectValueTypeVoid

Still the method you applied return Teste2(); already this marked not to return anything and therefore in the method will not be accepted any expression within return, then when you add Teste2() inside return it will be an expression, regardless of the type of value that Test2() return.

You could even do something like public in Teste2() (even though nay makes sense, this is only to explain where occurs the "evaluation") that would get the same error:

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

Note: the 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`

1

What happens is that the instruction Return ends the execution of the method in which it appears and returns the control for the call method. It can also return an optional value. If the method is a type void, the instruction Return may be omitted and at the time of using in a return method void, the use of Return will cause nothing below it to run and thus returning control to the call method.

In your case you can make a call to your Teste1 method before the instruction Return

And you can’t do return Teste1() for the 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;
    }

Basically we could say that this can’t happen because it’s just like the C# was specified by the development team.

  • I think you understand the motivation of the question.

  • I tried to contextualize the pq it can not do the Return Teste1().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.