Category: c#

Xamarin Forms – check if phone is currently connected to Internet or not

Sometimes, you may need to check if your application has internet connection or not. In order to do so, you can use the plugin and then using the following method: public async Task HasInternet() {     var canReach = await CrossConnectivity.Current.IsRemoteReachable(“”);     return canReach; }

Print class name, method name for debugging purpose c#

– For normal purpose: Debug.WriteLine(this.GetType().Name + ” – ” + MethodBase.GetCurrentMethod() + ” – ” + e.Message + ” – ” + e.GetType()); – For catching an exception: Debug.WriteLine(this.GetType().Name + ” – ” + MethodBase.GetCurrentMethod() + ” – ” + e.Message + ” – ” + e.GetType()); Explaination: this.GetType().Name : will print the calling class.


Some packages to be installed for your .NET web services application

Here are some of my collections. Install-Package Microsoft.AspNet.Web.Optimization Install-Package Microsoft.AspNet.Mvc -Version Install-Package Microsoft.AspNet.WebApi.Core -version 5.2.3 Install-Package Microsoft.AspNet.WebApi.WebHost Install-Package EntityFramework

c# – Linq – JToken, JObject, JProperty, JArray

The JToken hierarchy looks like this: JToken – is an abstract base class JContainer – is an abstract base class of JTokens that can contain another JTokens JArray — represents a Json array (contains an ordered list of JTokens) JObject — represents a Json object ( contains a collection of JProperties) JProperty — represents a …

C# “?.” null-conditional , “??” null-coalescing operators

Just start learning something by looking at the example and rewrite it 1. ?. null-conditional operator Example: var Child = grandparent?.parent?.child; if (Child == null) Child = “”; What does this mean? First, “var” is to auto define the type for Child object which has same type of child. Second, the ?. will help you incase a object …

c# – parsing string remove white spaces

1- Use split function This string: string s = “hello hi 123 test abc” -> var parts = s.Split(‘ ‘); You will get parts[0]=”hello” parts[1]=”hi” parts[2]=”123″ parts[3]=”test” parts[4]=”abc” 2- Use split function with option This string: string s = “hello   hi  123 test       abc” -> var parts = s.Split(new char[] {‘ …


c# – run a code after a time delay

Sometimes, you will need to execute a piece of code after a time delay. To achieve this behaviour, we can have following 2 solutions: Solution 1: – We can use dependency service to implement a native function of delay for each platform (like delay Runnable for Android) Solution 2: – If you want a cross …