none
How the "void" keyword works RRS feed

  • Question

  • Hi all,

    I try to have a deeper understanding of the .net framework but I'm stuck on the 1st issue on my list - how void works.

    This is the System.Void structure as found on the void.cs file (referencesource dot microsoft dot com) :

    // ==++==
    // 
    //   Copyright (c) Microsoft Corporation.  All rights reserved.
    // 
    // ==--==
    ////////////////////////////////////////////////////////////////////////////////
    // Void
    //    This class represents the void return type
    ////////////////////////////////////////////////////////////////////////////////
    namespace System {
       
        using System;
        [Serializable]
        [System.Runtime.InteropServices.ComVisible(true)]
        public struct Void 
        {
        }
    }
    


    The tooltip when I hover the mouse over the void keyword in VS is "struct System.Void Specifies a return value type for a method that does not return a value."

     

    So I created my own little similar struct called Nothing in a file nothing.cs:

    namespace System {
       
        using System;
       /// <summary>
       /// Specifies a return value type for a method that does not return a value.
       /// </summary>
        [Serializable]
        [System.Runtime.InteropServices.ComVisible(true)]
        public struct Nothing 
        {
        }
    }
    


    And in another class file I use it:

    using System;

    using Linq;

    using ...etc

    using nothing = System.Nothing;

    namespace Test

    {

       class Class1

        {

             static nothing Main(string[] args); {}

        {

    }

     

    But this won't work:  Test.Class1.Main() : not all code paths return a value.

     

    Q1:  I don't see the tooltip in the void.cs code so the first question would be if the code at referencesource dot microsoft dot com is complete

    Q2: I had to declare the keyword "nothing" in my test; where is the keyword "void" defined in .net framework? Can't find it anywhere on referencesource dot microsoft dot com

    Q3: Why do my methods require something in return since the return reference is a struct similar to that of System.Void ? I feel that I miss something fundamental.

    Thank you!

     

    Thursday, July 16, 2015 3:46 PM

Answers

  • "Q1:  I don't see the tooltip in the void.cs code so the first question would be if the code at referencesource dot microsoft dot com is complete"

    The source is complete but the tooltip doesn't come from the source, it comes from the xml documentation files that aren't generated from the source.

    "Q2: I had to declare the keyword "nothing" in my test; where is the keyword "void" defined in .net framework? Can't find it anywhere on referencesource dot microsoft dot com"

    As already pointed out by RJP1973 the void keyword is defined by the language, not by the framework.

    "Q3: Why do my methods require something in return since the return reference is a struct similar to that of System.Void ? I feel that I miss something fundamental."

    Because the void type is treated specially by the compiler and the runtime and there's no way to get such special treatment to be applied to your own type even if it looks similar to void. void is intended to represent the absence of a value and that's not something that can be encoded in normal code.

    Part of this special treatment also involves ensuring that a value of type void can't ever exist. The compiler prevents you from declaring any variables of type void, using new void() to create an instance of void. The runtime prevents void instantiation by means of reflection - Activator.CreateInstance(typeof(void)) fails.

    "I didn't know that a Void struct existed, but I wonder if some special mapping is going on, in a similar way to how c# maps the "int" keyword to System.Int32."

    Yes, void is an alias for System.Void much like int is an alias for System.Int32. For example Console.WriteLine(typeof(void)) prints System.Void. However, C# generally prevents access to System.Void unlike in the case of System.Int32. You can't write System.Void Main() {...} nor you can write typeof(System.Void).

    Thursday, July 16, 2015 4:42 PM
    Moderator

All replies

  • This is an interesting question, and although I don't know the answer, I feel that the "void" as a keyword would not be a part of the .net framework itself but part of the c# language.

    I didn't know that a Void struct existed, but I wonder if some special mapping is going on, in a similar way to how c# maps the "int" keyword to System.Int32.

    My reasoning is that, Void struct aside, how you define a function with no return value is going to differ between different .net languages. In c# you use the void keyword but in Visual Basic you declare a Subroutine rather than a Function (if I remember correctly).


    • Edited by RJP1973 Thursday, July 16, 2015 4:16 PM
    Thursday, July 16, 2015 4:14 PM
  • "Q1:  I don't see the tooltip in the void.cs code so the first question would be if the code at referencesource dot microsoft dot com is complete"

    The source is complete but the tooltip doesn't come from the source, it comes from the xml documentation files that aren't generated from the source.

    "Q2: I had to declare the keyword "nothing" in my test; where is the keyword "void" defined in .net framework? Can't find it anywhere on referencesource dot microsoft dot com"

    As already pointed out by RJP1973 the void keyword is defined by the language, not by the framework.

    "Q3: Why do my methods require something in return since the return reference is a struct similar to that of System.Void ? I feel that I miss something fundamental."

    Because the void type is treated specially by the compiler and the runtime and there's no way to get such special treatment to be applied to your own type even if it looks similar to void. void is intended to represent the absence of a value and that's not something that can be encoded in normal code.

    Part of this special treatment also involves ensuring that a value of type void can't ever exist. The compiler prevents you from declaring any variables of type void, using new void() to create an instance of void. The runtime prevents void instantiation by means of reflection - Activator.CreateInstance(typeof(void)) fails.

    "I didn't know that a Void struct existed, but I wonder if some special mapping is going on, in a similar way to how c# maps the "int" keyword to System.Int32."

    Yes, void is an alias for System.Void much like int is an alias for System.Int32. For example Console.WriteLine(typeof(void)) prints System.Void. However, C# generally prevents access to System.Void unlike in the case of System.Int32. You can't write System.Void Main() {...} nor you can write typeof(System.Void).

    Thursday, July 16, 2015 4:42 PM
    Moderator
  • Thank you RJP for your answer and thank you Mike for enlighten me on this issue!
    Thursday, July 16, 2015 6:50 PM