C# void type- safety

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

提交回复
热议问题