C# void type- safety

前端 未结 5 2017
眼角桃花
眼角桃花 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:40

    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.

提交回复
热议问题