I run the following code snippet.
Public Shared Sub DisplayDnsConfiguration()
Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
Dim adapter As NetworkInterface
For Each adapter In adapters
Dim properties As IPInterfaceProperties = adapter.GetIPProperties()
Console.WriteLine(adapter.Description)
Console.WriteLine(" DNS suffix................................. :{0}", properties.DnsSuffix)
Console.WriteLine(" DNS enabled ............................. : {0}", properties.IsDnsEnabled)
Console.WriteLine(" Dynamically configured DNS .............. : {0}", properties.IsDynamicDnsEnabled)
Next adapter
End Sub 'DisplayDnsConfiguration
I understand the end result, some properties are printed. However I don’t understand the way there.
The following three lines are the ones I don’t understand:
1. Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
2. Dim adapter As NetworkInterface
3. For Each adapter In adapters
Regarding line 1, why is the As NetworkInterface(), I understand when it is string or integer etc.. but not NetworkInterface(). Then I assume it populate the “adapters” with the data returned from the GetAllNetworkInterfaces() method.
Regarding 2. Same as above, but why doesn’t it use () in the end?
Regarding 3. Why use the adapter In adapters? What does it actually do? I understand it loops through the interfaces, but how?
Thanks
declares a variable called
yourIndividualNamewhich type isSomeType.intandStringare rather primitive types compared to aNetworkInterface, but declaring them is the same.For example:
To declare an array (a list) simply add
()to the typeBack to your question:
1:
Declares a list (actually an array) of
NetworkInterfacecalledadapters, but the list is empty. Then= NetworkInterface.GetAllNetworkInterfaces()fills that list.2:
This declares one empty variable of type
NetworkInterface. It is later used to go through the list.3:
This takes the first element in the list
adapters, saves it inadapterand does the stuff betweenForandNext. When there is another item in the list,Fortakes the next one, saves it inadapterand so on, until it reached the end of the listadapters. BetweenForandNextyou can useadapterto e.g. display some of its properties etc.A shorter version that does the same: