Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is System.Void?

Tags:

c#

.net

void

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?

like image 415
ordag Avatar asked Mar 27 '11 17:03

ordag


People also ask

What means system void?

void is just an indicator that function returns nothing at all.

Why is void used in C#?

You use void as the return type of a method (or a local function) to specify that the method doesn't return a value. You can also use void as a referent type to declare a pointer to an unknown type. For more information, see Pointer types. You cannot use void as the type of a variable.

What is void Visual Studio?

The Void structure is a specialized type used internally by the . NET Framework and particularly by Visual C# and Visual C++. It represents a return value type for a method that does not return a value. Visual Basic uses a Sub procedure when a value is not returned and a Function procedure when a value is returned.

What is a private void in C#?

private means the method, function, or property is not accessible to outside the class but can be invoke inside the class itself. public means the method, function, or property is accessible to outside the class and can also be invoke inside the class itself.


2 Answers

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.

like image 87
Jon Avatar answered Sep 21 '22 07:09

Jon


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.

like image 34
Cheng Chen Avatar answered Sep 19 '22 07:09

Cheng Chen