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
?
void is just an indicator that function returns nothing at all.
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.
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.
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.
From the documentation:
The
Void
structure is used in theSystem.Reflection
namespace, but is rarely useful in a typical application. TheVoid
structure has no members other than the ones all types inherit from theObject
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With