在隐式类型的局部变量声明中,局部变量的类型是通过局部声明语句右侧的初始值设定项表达式推断的。
例如,编译器推断以下所有变量声明的类型:
以下是引用片段: Dim population = 31719 Dim name = "Belize" Dim area = 1.9 Dim country = New Country With { .Name = "Palau", ...} |
因此,它们完全等效于以下显式类型声明:
以下是引用片段: Dim population As Integer = 31719 Dim name As String = "Belize" Dim area As Float = 1.9 Dim country As Country = New Country With { .Name = "Palau", ...} |
以下是引用片段: Dim country As Object = New Country With { .Name = "Palau", ... } |
推断类型可防止意外使用后期绑定,更重要的是,它允许为新数据类型(如 XML)绑定强大扩展,如下所示。
以下是引用片段: For Each country In smallCountries Console.WriteLine(country.Name) Next |
country 的类型被推断为 Country,即 SmallCountries 的元素类型。
评论 {{userinfo.comments}}
{{child.content}}
{{question.question}}
提交