Possible Duplicate:
What is System.Void?
I have no practical reason for knowing this answer, but I’m curious anyway…
In C#, trying to use System.Void will produce a compilation error:
error CS0673: System.Void cannot be used from C# — use typeof(void) to get the void type object
As I understood it, void is simply an alias of System.Void. So, I don’t understand why ‘System.Void’ can’t be used directly as you might with ‘string’ for ‘System.String’ for example. I would love to read an explanation for this!
Incidentally, System.Void can be successfully used with the Mono compiler, instead of Microsoft’s, and there it appears equivalent to using the void keyword. This must therefore be a compiler-enforced restriction rather than a CLR restriction, right?
I believe the sole purpose for this struct is to use it in reflection, whereas the other types (like
System.String,System.Int32etc.) are proper types holding data.System.Voidcarries no data and you cannot instantiate this struct from your code.My guess about the compiler error is that it’s there to enforce consistency in code. It would look weird to have methods like this:
At first glance, it appears to be returning something while in reality it doesn’t. In my opinion, this is a good decision by the C# team (if my speculation about it is correct)