What are the recommended practices for cross-platform library development with C# 9?

TN 1 Reputation point
2020-11-26T14:30:42.217+00:00

What features of C# 9 could be safely used:

  • in Xamarin (Android/iOS),
  • in "the legacy" .NET Framework (4.8+),
  • in "the obsoleted" .NET Standard 2.0/2.1?

Unfortunately, .NET 5 is not "the only one united framework".

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,395 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,274 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2020-11-29T12:59:16.737+00:00

    Hello @TN ,

    Note that the link provided by @Cole Xia (Shanghai Wicresoft Co,.Ltd.) shows

    <PropertyGroup>  
       <LangVersion>preview</LangVersion>  
    </PropertyGroup>  
    

    And

    <PropertyGroup>  
       <LangVersion>9.0</LangVersion>  
    </PropertyGroup>  
    

    While .NET 5 is out of preview so you can use

    <PropertyGroup>  
    	<OutputType>Exe</OutputType>  
    	<TargetFramework>net5.0</TargetFramework>  
    	<LangVersion>9.0</LangVersion>  
    </PropertyGroup>  
    

    Regarding what can be used, it's always best not to look for new features to use in a project just to use them but instead know what is available and then down the road know what is available to use but not just to use because it's new but instead a particular feature fits best into code rather than earlier methods in C#.

    Then there are some features that will be preference based e.g. new expressions where the first one is C# 9 and the former before C# 9

    List<Person> people = new();  
    List<Person> people1 = new List<Person>();  
    var people2 = new List<Person>();  
    

    Another example, iterating a range.

    Tried and true for any version of C#

    foreach (var contact in contacts)  
    {  
        Console.WriteLine(contact);  
    }  
    

    C# 9 is actually more code as a range needs to be an array rather than a list.

    foreach (var singleContact in contacts.ToArray()[..])  
    {  
        Console.WriteLine(singleContact);  
    }  
    

    But if we look at skipping the first element in C# 9 which is hard coded

    foreach (var singleContact in contacts.ToArray()[1..])  
    {  
        Console.WriteLine(singleContact);  
    }  
    

    To a dynamic version

    var startIndexer = new Index(1);  
    foreach (var singleContact in contacts.ToArray()[startIndexer..])  
    {  
        Console.WriteLine(singleContact);  
    }  
    

    The might be worthy but is still a preference.

    What seems like an excellent candidate is pattern matching to use and is fine cross platform since it's not dependent on any platform. For example a modified method from Microsoft code samples that is not an extension method turned into an extension method.

    public static class LanguageExtensions  
    {  
        /// <summary>  
        /// use Switch expression to determine where a int value falls  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <returns></returns>  
        public static string Determination(this int sender) => sender switch  
        {  
            <= 0 => "Less than or equal to 0",  
            > 0 and <= 10 => "More than 0 but less than or equal to 10",  
            _ => "More than 10"  
        };  
    }  
    

    Example usage

    int[] values = new[] { -1, 10, 0, 11, 1 };  
      
    foreach (var value in values)  
    {  
        Console.WriteLine($"{value,4} is {value.Determination()}");  
    }  
    

    Possible downside is when a team of developers there are some that have difficulties understanding Determination or have difficulties interpreting the method. One must decide between readability and function from conventional to new feature.

    Records may or may not suit a need and can see some developers making attempts to use them just to use them while the better choice is to understand records and know when they are a good candidate for a task.

    Hopefully this information is of use to you.

    0 comments No comments