What does a void subroutine return?

ⅰ亾dé卋堺 提交于 2019-12-02 10:29:12
Tim Schmelter

System.Void is not nothing , it's a struct hence a value type.

However, a method that is a Sub(VB.NET) or "returns" void(C#) does not really return a value. So you cannot write something like this:

System.Void nothing = Foo(); // Foo is a void-method 

This doesn't compile ("System.Void cannot be used from C# -- use typeof(void) to get the void type object"). Related: Why not System.Void?


As Jeroen and others have mentioned, actually a void method does really not return anything, so the correct answer was: "It returns nothing".

MSDN mentioned that it's only useful in reflection:

"The Void structure is used in the System.Reflection namespace, but is rarely useful in a typical application. The Void structure has no members other than the ones all types inherit from the Object class."

If you look at the tooltip on the void-keyword you see that it maps to System.Void. But again, that doesn't mean that it is returned from the method. It's just a placeholder for a non-existing return value. I guess that void also exists due to historical reasons since C# is based on C.

Also worth reading: Why does void in C mean not void?

A method, whose return type is void, it does not return anything. You could execute any statements you want inside the method's body. Furthermore this statements can affect anything you want, but at the end of the day your method will not return something.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!