Network - NKS0051
Use NetworkTools.NetworkAvailable
This analyzer provides the following strings:
Context | String |
---|---|
Error List | Caller uses |
Suggestion Box | Use |
Description | While |
Extended Description
This code analyzer detects the usage of GetIsNetworkAvailable
from the NetworkInterface
class found in the System.Net.NetworkInformation
namespace.
Normally, NetworkInterface.GetIsNetworkAvailable
would return true if there are network interfaces that are ready to connect (their state is UP). However, this approach doesn't work on Android systems as Nitrocid doesn't build for Xamarin.Android, which has the necessary libraries for Java bindings to the Android API.
As a result, you'll have to use the sensible NetworkTools.NetworkAvailable
property to allow you to check to see if there are ready network interfaces. Please note that if you have no working internet connection, even if your WiFi is turned on in your Android device, it'll report as false.
Analysis Comparison
To get a brief insight about how this analyzer works, compare the two code blocks shown to you below:
Before the fix
After the fix
Suppression
You can suppress this suggestion by including it in the appropriate place, whichever is convenient.
For more information about how to suppress any warning issued by the Nitrocid analyzer, visit the below page:
Recommendation
We recommend that every caller which use this function use the recommended abovementioned method.