Is 'void' a valid return value for a function?

c#, return, void

Solution

`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.

Problem

``` 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; } ```

Original source

Related problems