是否有可能在 Visual Basic 中得到 "use of uninitialized local variable" 警告?

Is it possible to get a "use of uninitialized local variable" warning in Visual Basic?

比较以下片段:

static void Main(string[] args)
{
    int i;
    Console.Write(i); // yields an error: Use of unassigned local variable 'i'
}
Public Sub Main()
    Dim i As Integer
    Console.Write(i)  ' no warning
End Sub
Public Sub Main()
    Dim s As String
    Console.Write(s)  ' yields a warning: Variable 's' is used before it has been assigned a value. A null reference exception could result at runtime.
End Sub

我喜欢最后一个示例中显示的警告,我也想在第二个示例(值类型)中使用它(尽管第一个示例中的文本比第三个示例中的文本更合适在这种情况下)。这可能吗?或者这是 VB.NET 中(故意)省略的功能?

不,这不可能。

以下是 Microsoft 在 2010 年 feature suggestion 闭幕中对此的评价:

Receiving warnings when you haven't used one of your locals is indeed valuable and we'd love to add this warning to Visual Basic. However, we must be careful as we introduce new warnings given the number of people who run with "warning-as-error" enabled. While this warning would often indicate a coding error, there may be instances within existing apps where an unused local is benign - left over from a previous version of the method. In these cases, we need to take great care not to break existing codebases when they are moved from one version of the language to the next.

因此,由于向后兼容的原因,他们似乎不会添加此功能。