0

I was just doing an assessment on pluralsight and was given the following question.

"What does a void subroutine return?"

I was under the impression that a void subroutine did not return anything, but this was not one of the answers provided (Multiple choice question).

Does .Net return a value type in the background or is this question incorrect?

The choices I was given were: Integer Boolean String Datetime

Community
  • 1
  • 1
  • 4
    [`System.Void`](https://msdn.microsoft.com/en-us/library/system.void.aspx) is not _nothing_ , it's a `struct` hence a value type. – Tim Schmelter Feb 13 '15 at 09:51
  • void subroutine return no data but the execution control only to calling routine. What choices they gave to select? – Adil Feb 13 '15 at 09:52
  • @TimSchmelter of course it's a struct. But his method will not return something to the caller? Correct? – Christos Feb 13 '15 at 09:54
  • 1
    If you add the multiple choices, we might deduce what direction the question wants you to go. – Jeroen Vannevel Feb 13 '15 at 09:55
  • 1
    Well, perhaps it is difficult to add *nothing* to a list of multiple-choice answers. – Hans Passant Feb 13 '15 at 10:00
  • @Christos: i've made my comment an answer.No, you cannot use the return value, the compiler simply forbids it to use `System.Void`. – Tim Schmelter Feb 13 '15 at 10:00
  • The `System.Void` structure allows describe methods in `System.Reflection` uniformly. I suspect also, it simplifies a type inference (implicitly typed variables). – Mark Shevchenko Feb 13 '15 at 10:05
  • But a "void subroutine" really does not return anything. There is no return value pushed onto the stack. I suppose the answer could be "According to reflection, it returns "System.Void", but in reality it returns nothing". – Matthew Watson Feb 13 '15 at 10:13

2 Answers2

6

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?

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • `System.Void` is not nothing, but it is also not what methods declared to return "void" actually return. If that was what they accepted as the correct answer, they've got a question to fix. – Jeroen Mostert Feb 13 '15 at 10:45
  • @JeroenMostert: i have edited my answer to emphasize this more. – Tim Schmelter Feb 13 '15 at 10:58
2

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.

Christos
  • 53,228
  • 8
  • 76
  • 108