Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VB.NET implicitly typed local variables

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the use of VB.NET implicit type local variables, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Our 51CTO editors have introduced you to the basic concepts of Linq implicitly typed local variables and .NET Framework implicitly typed variables.

In the VB.NET implicit type local variable declaration, the type of the local variable is inferred from the initializer expression to the right of the local declaration statement.

For example, the compiler infers the types of all of the following variable declarations:

The following is a reference clip:

Dim population = 31719

Dim name = "Belize"

Dim area = 1.9

Dim country = New

Country With {

.Name = "Palau",...}

Therefore, they are completely equivalent to the following explicit type declarations:

The following is a reference clip:

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",...}

Because the type declared by the VB.NET implicit type local variable is inferred from the new Option Infer On (the default value for the new project), access to such variables is always early bound regardless of the setting of Option Strict. Programmers must explicitly specify late binding in Visual Basic 9.0 by explicitly declaring variables of type Object, as follows:

The following is a reference clip:

Dim country As Object =

New Country With {

.Name = "Palau",...}

Inference types prevent the accidental use of late binding and, more importantly, allow powerful extensions to be bound to new data types, such as XML, as shown below.

Loop control variables in For...Next or For Each...Next statements can also be variables of implicit type. When you specify a loop control variable, as shown in For I = 0 To SmallCountries.Count or For Each country In smallCountries, the identifier defines a new implicitly typed local variable whose type is inferred from the initializer or collection expression and acts on the entire loop. By applying this type inference, you can rewrite the cycle that prints all small countries, as follows:

The following is a reference clip:

For Each country

In smallCountries

Console.WriteLine

(country.Name)

Next

The type of country is inferred to be Country, which is the element type of SmallCountries.

After reading the above, do you have any further understanding of the use of VB.NET implicit type local variables? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report