Pattern matching

Pattern matching is used for:

  • testing expression to check if some value have certain characteristics
  • safer compare because ‘==’ and ‘!=’ can be overloaded

Examples:

a is int number
sequence is IList<T> list

switch statement:

command switch
{
   Operation.SystemTest => RunDiagnostics(),
   Operation.Start => StartSystem(),
   Operation.Stop => StopSystem(),
   Operation.Reset => ResetToReady(),
   _ => throw new ArgumentException("Invalid enum value for command", nameof(command)),
};

switch with ranges:

tempInFahrenheit switch
{
    (> 32) and (< 212) => "liquid",
    < 32 => "solid",
    > 212 => "gas",
    32 => "solid/liquid transition",
    212 => "liquid / gas transition",
};

collection pattern matching:

array is [1,1,2,3,5,8, ..]

Further read:

https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching

Automatic scrolling to edited text box in Xamarin Android.

Important note: this should work well with newer version of Xamarin (2.3.3). If you cannot update for some reason this article will explain workaround.

Introduction

In previous article I explained how to detect show and hide events of software keyboard in Xamarin Android. I did it in my project because of the fact that some of text boxes was hidden by software keyboard that suppose to edit contents of those text boxes. In this article I will explain how to work around this issue in some older versions of Xamarin. Continue reading “Automatic scrolling to edited text box in Xamarin Android.”