Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

return

void

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;
}
like image 253
Alexander Avatar asked Jan 28 '14 10:01

Alexander


1 Answers

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.

like image 99
akton Avatar answered Oct 25 '22 07:10

akton