C# void type- safety

前端 未结 5 1853
眼角桃花
眼角桃花 2021-02-10 04:24

I am just new for C# programming (coming from C++)

My question is: In C# every type inherits from Object Why \'void\' doesn`t?Can it cause some RT/type safety problems

相关标签:
5条回答
  • 2021-02-10 04:35

    Unlike C++, C# allows the void keyword only to specify that a method does not have a return type.

    In C#, the void keyword is invalid as a method parameter. In C++, however, void can be used to specify a "universal" pointer.

    One caveat to this rule is when using unsafe code, which is generally unnecessary in C#.

    EDIT: Contrary to what some others have posted, there's a difference between void and System.Void. There is a void keyword, in addition to the struct System.Void. The keyword is used to indicate that a method does not have a return type. System.Void, on the other hand, is used in reflection to ask which type a method returns (there may be other uses for System.Void, but I'm not aware of any).

    C# void keyword
    C++ void keyword

    0 讨论(0)
  • 2021-02-10 04:37

    Void is a Type, but methods that return void, don't push anything onto the stack. System.Void is declared as a struct, so it should inherit from object, too, but it's handled differently. You can see it clearer if you look at the IL code, after a method returning void is called, there is no pop instruction, which normally is used to pop unused return values off the stack.

    EDIT: Actually, i checked. If i declare a method as returning System.Void, i get an error message stating that System.Void can't be used from C# code. But try the following:

    typeof(object).IsAssignableFrom(typeof(void))
    typeof(System.ValueType).IsAssignableFrom(typeof(void))
    

    that will return true, so void does derive from object and System.ValueType (i.e. struct)

    0 讨论(0)
  • 2021-02-10 04:40

    Void is not a type of object. It's a type of nothing. At least in C#, just about the only time you use the word void is as a function return type, so it means the function returns nothing. Non-existence of something can't derive from anything. The only other place to use it is in unsafe context, referring to pointers, which also do not derive from Object.

    0 讨论(0)
  • In C#, void is a keyword that is used in two contexts:

    • In the context of method signatures, void indicates the absence of a return type; it doesn't refer to a type "void" which could inherit from object.

      (In reflection, a return type has always to be specified. For this reason, there is a System.Void type in the framework, but void and System.Void cannot be used interchangeably.)

    • In unsafe context, void* is a pointer type to an unknown type. As mentioned in the blog post linked by @Colin Mackay, pointer types do not inherit from object, although they can be converted to one.

    0 讨论(0)
  • 2021-02-10 04:49

    Not everything inherits from Object - That is a common myth. Interfaces don't inherit from Object.

    See this for more details: http://blogs.msdn.com/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx

    0 讨论(0)
提交回复
热议问题