Skip to content Skip to sidebar Skip to footer

Convert From Kotlin To C#

I need to convert the following code from kotlin to c#. kotlin code: fun isConnectedToTheInternet(): Boolean { val cm = application.getSystemService(Context.CONNECTIVITY_SE

Solution 1:

First import android content as below

using Android.Content;

Then declare connectivity manager as below

ConnectivityManagercm= (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);

and the first block of code would be translate as below.

return cm.GetNetworkCapabilities(cm.ActiveNetwork).HasTransport(Android.Net.TransportType.Wifi) || cm.GetNetworkCapabilities(cm.ActiveNetwork).HasTransport(Android.Net.TransportType.Cellular) || cm.GetNetworkCapabilities(cm.ActiveNetwork).HasTransport(Android.Net.TransportType.Ethernet);

and the second block of code would be translate as below

return cm.ActiveNetworkInfo.Type  ==  ConnectivityType.Wifi ||
          cm.ActiveNetworkInfo.Type == ConnectivityType.Mobile;

Run doesn't exists in xamarin because is not a part of the android sdk. In fact is part of the kotlin scope functions.Take a look at the official documentation of kotlin.

PD: I code in kotlin everyday for my job and i code in c# for hobbie :)

Post a Comment for "Convert From Kotlin To C#"