39

I am curious if there is a legitimate reason as to why C# does not support calling a void method as part of the return statement when the calling method's return type is also void.

public void MethodA() 
{ 
    return; 
}

public void MethodB() 
{
    return MethodA();
}

So we would normally see this:

public void MethodMeh() 
{
    if (expression) 
    {
        MethodA();
        return;
    }

    // Do more stuff
}

... when we could be using this instead:

public void MethodAwesome() 
{
    if (expression) 
        return MethodA();

    // Do more stuff
}

Is this a language limitation due to how C# handles void?

18
  • 4
    I'm sure it's in the language spec. It would be very misleading to allow the return keyword in a context where no value is returned.
    – Eric J.
    Jul 10, 2014 at 0:17
  • 3
    its like saying - "in this method which does not return a value,lets return this method(which also does not return)...and so on,and on..." Jul 10, 2014 at 0:22
  • 35
    None of the above examples returns a method!
    – user1818839
    Jul 10, 2014 at 7:54
  • 5
    Why would you want to do MethodAwesome? Shorter code is not better code, clearer code is better code. It looks like it returns a method when it wouldn't be, that is unnessarily confusing.
    – JamesRyan
    Jul 10, 2014 at 10:13
  • 6
    +1. Actually, this question could be extended to "why is it not legal to write return void; in a method returning a void?". And then, by extension, return f() if f's return type is void. The problem I see - which may actually imply the answer to the question - is that it may lead to the question "Then why is it not legal to write a variable declaration like void x = f()?".
    – CompuChip
    Jul 10, 2014 at 14:12

9 Answers 9

39

Because it's simply the way the language is defined.

A method can use return statements to return control to its caller. In a method returning void, return statements cannot specify an expression. In a method returning non-void, return statements must include an expression that computes the return value.

It's an arbitrary decision (presumably made for compatibility with ANSI C and its other descendants), and other languages do things differently.

For example, in Python, all functions return a value. If you execute a return statement without a value, or let control reach the end of the function, then it's just like you had written return None.

In contrast, Pascal limits the terminology of function to subprograms that have a return value; if you don't want to return anything, you use a procedure instead.

3
  • 10
    Actually, C++ does allow it, for template reasons.
    – MSalters
    Jul 10, 2014 at 8:28
  • @msalters only in certain contexts. Outside of those, not allowed. Jul 10, 2014 at 23:19
  • I think this is going to be the most appropriate answer. I was mostly curious if it was completely arbitrary or something that simply couldn't be supported (now or in the future) within the language architecture. Jul 11, 2014 at 17:18
28

void is the absence of information; it doesn’t make any sense to return it. It’s not a type*, and it’s not a value, unlike in some other languages. No, that’s not really a limitation.

* Well, sort of, as @Lucas points out. It’s an informational type; it doesn’t actually represent a type in the usual sense. You can’t have one.

18
  • Aren't we already returning void when we use return; inside of a method that has a void return type? Jul 10, 2014 at 0:20
  • 3
    @MattBeckman: No, you’re just returning. VB.NET makes this distinction clearer.
    – Ry-
    Jul 10, 2014 at 0:21
  • @MattBeckman, note that return without an expression afterwards is not valid for a method with a return type. Clearly it returns nothing. Jul 10, 2014 at 0:22
  • Aye, but we're returning nothing. But isn't "nothing" the same as "absence of information" or "void"? Jul 10, 2014 at 0:23
  • 5
    In C++, it's really a limitation in templated scenarios, so it's allowed in those cases. The unit type with only a single value instead of void would've been a better solution, of course—the resulting code would've been completely the same.
    – Joker_vD
    Jul 10, 2014 at 6:40
7

Your question is pretty much about the difference between the void type and the unit type (most commonly seen in functional languages like F#).

Basically, void is not a real type. While there is System.Void for reflection purposes, you can't use void in most places where you can use a real type: you can't have a variable of type void and you can't use it in generics (even though being able to write Func<void> would be sometimes very useful).

On the other hand, unit in F# is a real type: it has a (single) value (called ()), you can write the equivalent of Func<void> (written unit -> unit, where the first unit means “no parameters”, similar to writing void in the parameter list in C) and you can have variables of type unit. For example:

let unitTest (f : unit -> unit) =
    let nothing : unit = f()
    ()

The last line actually shows that in F#, you sometimes have to explicitly return unit.

3
  • 2
    Func<void> is also called Action :)
    – Luaan
    Jul 11, 2014 at 8:53
  • @Luaan That's exactly the problem. It would be nice if methods like Task.Factory.StartNew() didn't have to have separate overloads for Func<T> and for Action.
    – svick
    Jul 11, 2014 at 9:36
  • Well, that would have to extend through a lot of the infrastructure too - e.g. Task<unit> etc. It nicely shows that C# wasn't much of a functional language in the beginning. Now it has to keep backward compatibility while introducing many functional paradigms :)
    – Luaan
    Jul 11, 2014 at 9:44
4

This is not allowed according to the language specification. From 15.9.4 of ECMA-334 (emphasis mine):

A return statement with an expression can only be used in a function member that computes a value, that is, a method with a non-void return type, the get accessor of a property or indexer, or a user-defined operator.

4

It comes down to choices by the language designer.

From a type perspective, void has no enumerable values, so it doesn't make sense to return a value of "void" type? void is absence of type or evaluation context.

You cannot instantiate or return "void" in C# or Java.

If you could do so, then you should also be able to say:

 void i;
 i = (what would go here?)
 return i;

The above doesn't make any sense, but is equivalent to what you propose.

In the end, proposing that we could propagate void return context with the return statement is simply a matter of syntactical sugar, which comes down to choices made by the language designer.

The C# language spec http://msdn.microsoft.com/en-us/library/aa691305(v=vs.71).aspx (section 7.1) says

Nothing. This occurs when the expression is an invocation of a method with a return type of void. An expression classified as nothing is only valid in the context of a statement-expression (Section 8.6).

On the otherhand, the C++ designer actually chose to allow just the construct you propose, but it was specifically for syntactical uniformity in templates. In C++ the return type of a template function can be a template parameter. (Stroustrop C++ Programming Language p 148) Without it, there would be common cases that would compile for all types, but not for void, such as nested function calls of type T. For that reason, the following is valid C++:

void B() {
  return;
}

void A() {
  return B(); // legal C++ - doesn't yield a value
}

// But even in C++ the next line is illegal
int i = A();

So the statement expression "return B();" where B is a void function is only a shortcut for "B(); return;" and doesn't actually yield a value, because there is no such thing as a void value.

4
  • 3
    What would go there? An expression of type void, of course. Like a call to a void function. Now, I can't see why assigning a void to a variable would ever be useful, but the point of the OP's question is that it could have been implemented.
    – dan04
    Jul 10, 2014 at 0:45
  • @dan04 Yeah, but "not useful" is actually a good explanation all in itself. If not for the shortcut of not actually returning anything, there would be no need for void at all. It's simply a syntactic construct to allow specifying procedures (no return value), as well as functions (a single return value). Of course you could make a function that always returns null of some unit type. But what would be the point? It's not null, it's "nothing". What would be the point of having language features that increase complexity for everyone yet offer no functionality whatsoever?
    – Luaan
    Jul 10, 2014 at 7:51
  • Actually, return MethodA(); is valid C++. Jul 10, 2014 at 8:41
  • @dan04 - If you show me "an expression of type void", I'll show you "null". It makes no sense to assign void. You can only assign values. Void is not a value.
    – mrjoltcola
    Jul 10, 2014 at 13:08
2

I have a guess about a legitimate reason for this.

When the compiler sees an expression, it tries to match it to something from a list of known expressions. You could say that there are two kinds of statements that start with return:

  1. return expr where expr is an expression assignable to the declared return type of the containing method
  2. return without anything else

These are entirely different statements, even if they look similar. But one can compile to the other, in certain cases (just like foreach compiles to a while plus stuff). So, you could add a rule that says that return expr compiles to whatever expr; return; compiles to, whenever expr is determined to be of no return type (that is, void).

But then, the implementers of C# compilers would have to allow for all expressions to have no return type, when normally only statements are allowed to have no return type. If they did this, then they would have to manually disallow expressions of no type wherever an actual type is required. For example, in the expressions that act as arguments to a function call MyMethod(1, 2, SomethingVoid()), the compiler would additionally have to check whether any of these expressions does not return a type. And for every new kind of expression added to C# in the future, the same check would have to be added.

The above is possible, but it would happen for no reason other than to allow non-statement expressions in the abstract syntax tree (that's generated during compilation) to have no return type, and that would be the case just to allow for a minor syntactic structure - and one that has an alternative which is no longer (in keystrokes) to type and possibly clearer to read.

In the end, it doesn't sound like it's worth it.

1
  • 1
    "they did this, then they would have to manually disallow expressions of no type wherever an actual type is required" - not really no. You can treat unit as a normal type that's a singleton. For MyMethod(1, 2, SomethingVoid()) the compiler already has to check whether 1 (an int) is a valid type for the first parameter, etc. As a matter of fact it could be easier the other way around, because right now you probably really have an additional code path for void or needing a more complicated general algorithm to handle it as well.
    – Voo
    Jul 10, 2014 at 22:59
2

You're actual question title doesn't appear to be covered by the answers, although they may be getting at the correct thing. You're question title

Why does C# not support returning a method with a void return type

Actually C# does allow this, just not in the format you're trying. Using the void keyword as others have answered means that the method returns nothing. If you want to return a method, that returns nothing then you want to do something like this:

public Action MethodExample()
{
    return () => { Console.WriteLine("Hello World!"); };
}

Then a simple call will give you this action:

var action = MethodExample();
action(); // prints Hello World
2
  • 1
    If you take the title literally, that is what it asks for. But from the code in the question, it's clear this isn't what the OP actually meant.
    – svick
    Jul 10, 2014 at 17:42
  • @svick in which case the question title probably needs improving. My answer does address the question in the title and hopefully helps anyone who legitimately has that issue so not sure it deserves a down vote. I have mentioned that the other answers provide better information for the question detail, this just adds to that.
    – Ian
    Jul 10, 2014 at 19:42
1

A void is empty. The void keyword in the C# language indicates that a method returns nothing. When a void method is invoked, it has no result and no variable can be assigned.

see this explanation

-1

Correct me if i am wrong but i think its because:

public void MethodA() 
{ 
    return; 
}

public void MethodB() 
{
    return MethodA();
}

would be the same like(which are legal):

public void MethodA() 
{ 

}

public void MethodB() 
{
    MethodA();
}

And also:

public void MethodMeh() 
{
    if (expression) 
    {
        MethodA();
    }else{
    // do more stuff
    }
}
2
  • 1
    In his examples return VoidMethod(); stops the execution while replacing with a simple method call does not.
    – dee-see
    Jul 10, 2014 at 17:41
  • Isn´t the execution stopped anyway, because its the last line in that method?
    – b1ub
    Jul 15, 2014 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.