question

t-nt avatar image
0 Votes"
t-nt asked RobCaplan edited

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

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".

dotnet-csharpdotnet-standard
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@t-nt
C# 9.0 is only available with .NET 5 ,check the article here to access new C# 9 features .


0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

Hello @t-nt,

Note that the link provided by @ColeXia-MSFT 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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.