7
private void SaveMoney(string id...)
{
    ...
}

public void DoSthWithMoney(string action,string id...)
{
    if(action=="save") return SaveMoney(string id);
    ...
}

Why won't C# let me return the void of the private function back through the public function? It even is the same data type "void"...

Or isn't void a data type?

Is the following code really the shortest workaround?

if(action=="save") {
    SaveMoney(string id);
    return;
}
cvbarros
  • 1,684
  • 1
  • 12
  • 19
Alexander
  • 19,906
  • 19
  • 75
  • 162
  • 2
    Possible duplicate of http://stackoverflow.com/questions/1043034/what-does-void-mean-in-c-c-and-c – Naveen Jan 28 '14 at 10:48
  • 1
    Literally never considered this, it's a neat idea if you're lazy :D – Rawling Jan 28 '14 at 10:49
  • void means nothing, so you don't return anything, just use the return statement on its own. – Mikey Mouse Jan 28 '14 at 10:49
  • @Naveen Note that the code here is chaining a return of a function that also returns void, that isn't really a duplicate. – nos Jan 28 '14 at 10:49
  • 2
    void is not a data type per se (void* is a different matter). If a function returns a void it means it does not return a value ( the compiler will not take steps to place any value in the registers specified in the calling convention). So what you are trying here is not correct !!! – Pandrei Jan 28 '14 at 10:50
  • 1
    I don't see any reason why the compiler couldn't permit this; the compiler knows precisely what is happening here. It seems to just be the case that few would try to do this. – Mr. Smith Jan 28 '14 at 10:52
  • @nos His question is pointing basically at "what is void", so i added "possible" :). – Naveen Jan 28 '14 at 10:52
  • 2
    @Mr.Smith The compiler *could* know - but it's just a matter of language definition. In a function declared `void` (not *returning* `void` - you can't return a `void`) you may not `return` a value (and if you were able to write it as above, `void` would be considered a value, which it is not). – Thorsten Dittmar Jan 28 '14 at 10:55

5 Answers5

6

void is not a type in C#. In this instance, void means the absence of a return type or value so you cannot use it with return as you have in the first example.

This is different to C, for example, where void can mean typeless or an unknown type.

Community
  • 1
  • 1
akton
  • 14,148
  • 3
  • 43
  • 47
5

void is not an actual return (data)type! void says there is no result. So you can not return a value in a method that's declared void even though the method you're calling is also declared void.

I must admit it would be a nice shortcut, but it's not how things work :-)


Just an additional thought: If what you want was allowed, void would become both a data type and also the only possible value of that data type, as return x; is defined as returning the value x to the caller. So return void; would return the value void to the caller - not possible by definition.

This is different for null for example, as null is a valid value for reference types.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • Why/how would `void` have value `void`? If it had nothing in it, wouldn't it be more or less a typeless array of size zero? – Alexander Jan 28 '14 at 11:06
  • 1
    One could argue this is a case of the [Is-ought fallacy](http://en.wikipedia.org/wiki/Is%E2%80%93ought_problem). C# **could** permit this special-case by rewriting `return SaveMoney(id);` to `{SaveMoney(id);return;}` for you at compile-time. We've seen similar rewrites with `foreach` (when used on arrays), and even some instances of `for`. It just may not be for the better... – Mr. Smith Jan 28 '14 at 11:06
  • @Alexander This thought was because the method declaration is defined as to declare the *type* being returned (or `void` if there's no return value). `return` is defined to be used standalone without a return value, or to return the *value*. This would make `void` both a type and a value. – Thorsten Dittmar Jan 28 '14 at 11:12
  • @Mr.Smith Of course this could be done. Calling extension methods is another example for syntactic sugar like that. I'm not saying it can't be done - I'm just saying that the use of the `void` keyword would not be clear anoymore. – Thorsten Dittmar Jan 28 '14 at 11:14
  • Not really, since I don't write `return void`, but `return someFuncThatReturnsVoid()`, which "returns a value of type void", so to say. – Alexander Jan 28 '14 at 11:14
  • @Alexander You don't write `return void`, but effectively that's what that call would result in, just like `return Math.Sqrt(4);` effectively results in `return 2.0;` :-) Still, even in your sample, `void` would be a "value of type void", which would make `void` both a type and a value. Again, I'm not saying that this couldn't be handled as a special case in the language definition, but I'm trying to give a possible explanation of why it hasn't been done. I guess it is in the same problem class as `NULL` in SQL :-) – Thorsten Dittmar Jan 28 '14 at 11:17
  • `Math.Sqrt()`, or let's just name it `someFuncThatReturnsFloat(4)`, returns 2.0, not `float`... and it may be defined that someFuncThatReturnsVoid() returns 42, not `void`. There is no definition that void is a valid value for datatype `void`. While there are always 256 valid values for a `byte`, and 2 valid values for a `bool`, you have two possibilities how many values there are for a `void` - 0 or 1. The space required to save that "value" is the same, whether it can have 0 or 1 different values, entropy is always 0 bits. The difference is that the 0-possible-values void cannot be returned. – Alexander Jan 28 '14 at 11:52
  • OK, let's agree that `void` doesn't necessarily have to be the value that is actually returned. It could also be a constant named `void.VALUE`. Still, `void` would become a data type. By definition it is not. Let's also agree that one *could* one day implement this as syntactic sugar. Currently this is not the case, so my answer is correct saying that `void` is not a data type and yes, your code is the shortest, while not the only possible solution to the "problem". – Thorsten Dittmar Jan 28 '14 at 12:05
  • @Alexander: This actually *is* possible in C++/CLI, which also is a .net language. Most probably it was adapted from pure C++. I assume the compiler does exactly what Mr. Smith proposed in his comment, making 2 separate commands out of this single line. I am missing that in C# now, when converting my code from C++/CLI. – Tobias Knauss Jul 12 '17 at 12:08
4

That is not a workaround, it is the right way to do it.

meilke
  • 3,280
  • 1
  • 15
  • 31
4

Even if this would compile I wouldn't recommend it. In such a small method, it's clear what's going on, but if it's a bigger method, the next programmer is going to see that, blink, think "I thought this was a void method" scroll up, confirm that, scroll back down, follow the SaveMoney method, find out it returns nothing, etc.

Your "workaround" makes that clear at a glance.

Mikey Mouse
  • 2,968
  • 2
  • 26
  • 44
1

Just change the method to a boolean and return 0.

sootie8
  • 48
  • 3