No warning in VB.NET when function has no return

前端 未结 5 2021
梦如初夏
梦如初夏 2020-12-06 19:47

Some comments on Stack Overflow question Why doesn\'t the C# compiler stop properties from referring to themselves? regarding warnings got me thinking about old issues that

5条回答
  •  天涯浪人
    2020-12-06 20:08

    The warning will only let you know when a function is going to return Nothing by default.

    You would get a warning if return value was of a reference type.

    But your function has a return value of a value type, and those cannot be Nothing. Therefore, no warning.

    This is because function name inside this very function acts as a result variable. You can return a value by assigning it to the function name instead of using Return. And all variables are initialized with default values, including the function-name variable. This is not the case in C, hence the different meaning of the warning.

    Compare this to using variables before initializing them:

    Dim x As Integer
    CallFunction(x)  'No warning, x is implicitly and properly initialized to 0.
    
    Dim y as Object
    CallFunction(y)  'A warning: variable used before a value is assigned to it
    

提交回复
热议问题