41

I know that methods declared with void does not return anything.

But it seems that in C#, void is more than just a keyword, but a real type.
void is an alias for System.Void, like int that is for System.Int32.

Why am I not allowed to use that type? It does not make any sense, but these are just some thoughts about the logic.

Neither

var nothing = new System.Void();

(which says I should use void (Not an alias?))
nor

var nothing = new void();

compiles.

It is also not possible to use something like that:

void GiveMeNothing() { }
void GiveMeNothingAgain()
{
    return GiveMeNothing();
}

So what's the point with System.Void?

Pang
  • 9,564
  • 146
  • 81
  • 122
ordag
  • 2,497
  • 5
  • 26
  • 35

4 Answers4

43

From the documentation:

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.

There's no reason really to use it in code.

Also:

var nothing = new void();

This doesn't compile for me. What do you mean when saying it "works"?

Update:

A method void Foo() does not return anything. System.Void is there so that if you ask (through Reflection) "what is the type of the return value of that method?", you can get the answer typeof(System.Void). There is no technical reason it could not return null instead, but that would introduce a special case in the Reflection API, and special cases are to be avoided if possible.

Finally, it is not legal for a program to contain the expression typeof(System.Void). However, that is a compiler-enforced restriction, not a CLR one. Indeed, if you try the allowed typeof(void) and look at its value in the debugger, you will see it is the same value it would be if typeof(System.Void) were legal.

Pang
  • 9,564
  • 146
  • 81
  • 122
Jon
  • 428,835
  • 81
  • 738
  • 806
  • so the developers did not thought through and even if they say me: _Your method returns a System.Void struct, you can not use it_? And for what is it used in `Reflection`? – ordag Mar 27 '11 at 17:09
  • 3
    @ordag: Your method does not return *anything*. `System.Void` is there so that if you ask "what is the type of the return value of `void Foo()`?", it can answer you with `typeof(System.Void)`. It has to answer you with *something*, after all. – Jon Mar 27 '11 at 17:11
  • @Jon no, i can not compile this, too. – ordag Mar 27 '11 at 17:11
  • 1
    Well it could answer with `null` instead, but that'd most likely lead to unnecessary special casing in reflection code. And it's important to distinguish the C# language/compiler disallowing something and the CLR itself disallowing it. There is a number of things just forbidden in C# and allowed in the CLR. – CodesInChaos Mar 27 '11 at 17:15
  • @ordag: It is not legal for *you* to write `typeof(System.Void)` in code. But if you do `Type t = typeof(void)`, then `t` holds the same value it would hold if `Type t = typeof(System.Void)` were legal. It's the *compiler* that doesn't allow you to do this; the CLR wouldn't have a problem with it. – Jon Mar 27 '11 at 17:16
  • So they could have followed the logic of types more, but left it as a _looks-like-but-is-not_ - type. (And the reason at all is just for getting the `Type` in reflection) – ordag Mar 27 '11 at 17:25
  • @ordag: Yes. You might argue it's not "pure" to do so, but I do believe they made the best decision from a practical standpoint. – Jon Mar 27 '11 at 17:28
  • 2
    I don't think it's just for `Type` reflection. I assume the CLR uses it in its meta-data too for representing the return type of a `void` method. The CLR has to represent that return type somehow after all. And having a pseudo-type for that is a natural choice. – CodesInChaos Mar 27 '11 at 17:34
19

void/System.Void is different from int/System.Int32, it's a special struct in C#, used for reflection only. See this example:

class Program
{
   public static void Main(string[] args)
   {
      Type voidType = typeof(Program).GetMethod("Main").ReturnType;
   }
}

There must be some type used to describe the return type of Main method here, that's why we have System.Void.

Pang
  • 9,564
  • 146
  • 81
  • 122
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • If void functions do not return a value and therefore have no return type, why shouldn't the call to MethodInfo.ReturnType return a null instance of Type? To me, Type t = null; means that describes no type at all. – Carvo Loco Oct 19 '17 at 11:16
  • @CarvoLoco Currently in .NET languages, I believe your suggestion is doable. But here `null` is a magic object, using a specific type makes everything clearer, and explicit. – Cheng Chen Oct 23 '17 at 02:20
10

We have used the following code

public Type GetType(object o)
{
    var type = o == null ? typeof(void) : o.GetType();
}

so that we can use the null object pattern. It's pretty good. This allows us to do stuff like

GetType(o).GetProperties().Select( .....

instead of putting guard clauses everywhere.

Pang
  • 9,564
  • 146
  • 81
  • 122
llewellyn falco
  • 2,281
  • 16
  • 13
8

Beyond not returning a value, very little definition is given of void (although void* gets some attention) in the language spec. This isn't really a language concern - although the CLI may define it further.

Ultimately though: because it has no meaning to do new void()

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900