Please explain how .NET works, using classes
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 h开发者_C百科ow?
Thanks
Dim yourIndividualName as SomeType
declares a variable called yourIndividualName
which type is SomeType
. int
and String
are rather primitive types compared to a NetworkInterface
, but declaring them is the same.
For example:
Dim someText as String
To declare an array (a list) simply add ()
to the type
Dim severalTexts as String()
Back to your question:
1:
Dim adapters As NetworkInterface()
Declares a list (actually an array) of NetworkInterface
called adapters
, but the list is empty. Then = NetworkInterface.GetAllNetworkInterfaces()
fills that list.
2:
Dim adapter As NetworkInterface
This declares one empty variable of type NetworkInterface
. It is later used to go through the list.
3:
For Each adapter In adapters
' do some stuff
Next adapter
This takes the first element in the list adapters
, saves it in adapter
and does the stuff between For
and Next
. When there is another item in the list, For
takes the next one, saves it in adapter
and so on, until it reached the end of the list adapters
. Between For
and Next
you can use adapter
to e.g. display some of its properties etc.
A shorter version that does the same:
For Each adapter as NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
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
1 - It is declaring "adapters" as an array of the type "NetworkInterface"
In vb.net arrays are declared and indexed with (...) syntax, in c# it is [...], so an array of strings would be
dim stringArray as string()
the same in c# would look like
string[] stringArray;
2 - This will be a range variable for the for each loop, to reference objects in the array, so it is not an array itself
3 - The foreach is saying "Loop through Adapter array, for each object found, assign the local variable Adapter the current value in the array, then run this block of code, repeat until we reach the end of the array"
First of all, most of the time, you would see Step 2 and 3 in one line like this
For each adapter as NetworkInterface in adapters
Next
To answer your concerns: Step 1 is a collection of networkinterfaces, while number 2 is a local var of the object NetworkInterface. Step 3 makes you cycle through all NetworkInterfaces that are present in the collection from Step 1.
精彩评论